对于为什么这个RSpec方法不起作用感到困惑。我在这里看到了答案:How to use RSpec's should_raise with any kind of exception?并尝试了所有提议的组合但由于某种原因,我仍然得到NoMethodError。
这是例外
Exception encountered: #<NoMethodError: undefined method `expect' for #<Class:0x007fa5bd8e4120>>
以下是方法:
describe "admin should not be accesible" do
expect { User.new(name: "Example Name", email: "email@example.org", password: "foobar", password_confirmation: "foobar", admin: "true") }.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
我之前收到此错误,因此我知道我的方法正在执行我想要的操作:
1) User admin should not be accesible
Failure/Error: hey = User.new(name: "Hello", email: "hey@hey.org", password: "foobar", password_confirmation: "foobar", admin: "true")
ActiveModel::MassAssignmentSecurity::Error:
Can't mass-assign protected attributes: admin
我正在跑步:
Rails 3上的RSpec 2.1.0,防护叉塞0.3.2和spork 0.9.0答案 0 :(得分:13)
it
区块!
describe "admin should not be accesible" do
it "should bla" do
expect { User.new(name: "Example Name", email: "email@example.org", password: "foobar", password_confirmation: "foobar", admin: "true") }.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
end