测试与 rspec-rails 3.0.1 和 shoulda 的关联不起作用 [英] Testing associations with rspec-rails 3.0.1 and shoulda doesn't work
问题描述
目前,使用 rspec-rails (2.14.2),我使用 shoulda (3.5.0) gem 测试模型规范中的关联,如下所示:
Currently, with rspec-rails (2.14.2), I test my associations in model specs with the shoulda (3.5.0) gem like so:
# 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
经过一些研究,我在尝试使关联相关的断言起作用时遇到了困难(它们似乎都失败了).
After some research, I hit a wall trying to make association-related assertions work (they all seem to fail).
错误信息:
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 的情况下测试关联吗?根据我所看到的期望"语法,这似乎不可能.
- 对于所有人来说,should gem 是否会与 rspec 3.0.1 冲突?有解决方法吗?
推荐答案
shouda-matchers 是提供关联、验证和其他匹配器的 gem.
shoulda-matchers is the gem that provides association, validation, and other matchers.
shoulda gem(我们也制作)用于将 shoulda-matchers 与 Test::Unit 一起使用(并且还提供了一些不错的东西,例如上下文和使用字符串作为测试名称的能力).但是如果你在 RSpec 上,你会想要使用 shoulda-matchers,而不是 shoulda.
The shoulda gem (which we also make) is for using shoulda-matchers with Test::Unit (and also provides some nice things like contexts and the ability to use strings as test names). But if you're on RSpec, you'll want to use shoulda-matchers, not shoulda.
这篇关于测试与 rspec-rails 3.0.1 和 shoulda 的关联不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!