在rspec异常处理之后,为'expect'获取'NoMethodError'

时间:2012-07-06 21:21:15

标签: ruby-on-rails-3 rspec

对于为什么这个RSpec方法不起作用感到困惑。我在这里看到了答案:How to use RSpec's should_raise with any kind of exception?并尝试了所有提议的组合但由于某种原因,我仍然得到NoM​​ethodError。

这是例外

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

1 个答案:

答案 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