use*_*088 5 ruby-on-rails rspec3
我正在尝试使用 rspec 3.0 测试 own_to 关联,但我不断遇到错误
NoMethodError:RSpec::Core 的未定义方法“belong_to”
it "should belong to a user" do
ride = Ride.new
user = User.new
user.rides << ride
expect(ride).to belong_to user
Run Code Online (Sandbox Code Playgroud)
结尾
我在 rspec 3.0 的文档中找不到任何用于测试高级关联的内容。请帮助!
您可以简单地检查关联:
it "belongs to a user" do
ride = Ride.new
user = User.new
user.rides << ride
expect(ride.user).to be user
end
Run Code Online (Sandbox Code Playgroud)
在这种情况下,be匹配器验证对象身份。所以当且仅当user对象具有相同的object_id. 在这种情况下,这就是您想要的,并且对于它的阅读方式具有语义意义。
| 归档时间: |
|
| 查看次数: |
4474 次 |
| 最近记录: |