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

ruby-on-rails – 与rspec-rails 3.0.1和shoulda的测试关联不起

发布时间:2020-12-17 03:49:08 所属栏目:百科 来源:网络整理
导读:目前,使用rspec-rails(2.14.2),我使用shoulda(3.5.0)gem测试模型规范中的关联,如下所示: # app/models/user.rbclass User ActiveRecord::Base belongs_to :schoolend# spec/models/user_spec.rbdescribe User do it { should belong_to :school }end 经过一
目前,使用rspec-rails(2.14.2),我使用shoulda(3.5.0)gem测试模型规范中的关联,如下所示:

# app/models/user.rb
class User < ActiveRecord::Base

  belongs_to :school

end

# spec/models/user_spec.rb
describe User do

  it { should belong_to :school }

end

经过一些研究,我试图让与关联相关的断言起作用(它们似乎都失败了).

错误信息:

1) User
     Failure/Error: it { is_expected.to belong_to :school }
     NoMethodError:
       undefined method `belong_to' for #<RSpec::ExampleGroups::School:0x007f8a4c7a68d0>
     # ./spec/models/user.rb:4:in `block (2 levels) in <top (required)>'

所以我的问题是:

>你可以在没有shoulda gem的情况下测试协会吗?根据我在“expect”语法中看到的内容,这似乎不可行.
>对于每个人,shoulda gem是否与rspec 3.0.1打破?有解决方法吗?

解决方法

shoulda-matchers是提供关联,验证和其他匹配器的宝石.

shoulda gem(我们也做)是使用带有Test :: Unit的shoulda-matcher(并且还提供了一些很好的东西,比如上下文和使用字符串作为测试名称的能力).但如果您使用的是RSpec,那么您将需要使用shoulda-matchers,而不是应该使用.

(编辑:李大同)

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

    推荐文章
      热点阅读