加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 为库模块添加rspec测试似乎没有提取期望和匹配

发布时间:2020-12-16 21:32:19 所属栏目:百科 来源:网络整理
导读:我在我的应用程序中添加了更多的rspec测试,并且想要测试一个ScoringMethods模块,它在/lib/scoring_methods.rb中.所以我添加了一个/ spec / lib目录,并在那里添加了scoring_methods_spec.rb.我需要spec_helper并设置描述块如下: require File.expand_path(Fi
我在我的应用程序中添加了更多的rspec测试,并且想要测试一个ScoringMethods模块,它在/lib/scoring_methods.rb中.所以我添加了一个/ spec / lib目录,并在那里添加了scoring_methods_spec.rb.我需要spec_helper并设置描述块如下:
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe ScoringMethods do

  describe "should have scorePublicContest method" do
    methods = ScoringMethods.instance_methods
    methods[0].should match(/scorePublicContest/)
  end
end

现在,方法[0]是一个字符串,并且公共方法名称与正则表达式匹配没有问题.而“spec_helper”的相对路径是正确的.

问题是整个安装程序似乎不使用rspec库.
运行示例产生:

./spec/lib/scoring_methods_spec.rb:7: undefined method `match' for Spec::Rails::Example::RailsExampleGroup::Subclass_1::Subclass_1:Class (NoMethodError)
     ...

整个期望和匹配者的支持似乎都是缺失的.为了测试我的假设,我通过将“is_instance_of”替换为“is_foobar_of”来改变工作帮助器规范.该测试简单地失败,并说“is_foobar_of”不是目标对象的方法;它,这整个Spec :: Rails :: Example …层次结构不存在.

我也试过使用其他匹配器.我试过“be_instance_of”等等.看来我没有正确地包括rspec库.

最后,ScoringMethods是一个模块,就像Helpers是模块一样.所以我认为可以测试一个模块(而不是类,如控制器和模型).

我非常感谢你对我做错了什么的想法.也许有更有效的测试库模块的方法?谢谢!

解决方法

您应该将测试块包含在“它”块中.例如:
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe ScoringMethods do

  describe "should have scorePublicContest method" do
    it "should have a scorePublicContest method" do
      methods = ScoringMethods.instance_methods
      methods[0].should match(/scorePublicContest/)
    end
  end
end

您将发现返回的方法名称不能保证按照文件中存在的顺序.

我们经常在测试模块时使用的模型是将模块包含在为测试创建的类(spec文件内)中,或者包含在规范本身中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读