我一直在研究Ruby on Rails教程。我遇到了一个问题,让a test通过该检查以引发一个质量分配安全异常。我不确定为什么我会让这个测试失败,或者如何修复它。
rspec的:
describe "accessible attributes" do
it "should not allow access to user_id" do
expect do
Micropost.new(user_id: user.id)
end.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
end
故障:
1) Micropost accessible attributes should not allow access to user_id
Failure/Error: expect { Micropost.new(user_id: user.id) }.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
expected ActiveModel::MassAssignmentSecurity::Error, got #<NoMethodError: undefined method `call' for #<RSpec::Expectations::ExpectationTarget:0x8af2bb8>>
# ./spec/models/micropost_spec.rb:23:in `block (3 levels) in <top (required)>
答案 0 :(得分:1)
尝试使用to
代替should
作为您期望的raise_error匹配器。
describe "accessible attributes" do
it "should not allow access to user_id" do
expect do
Micropost.new(user_id: user.id)
end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
end