2012-07-06 6 views
5

このRSpecメソッドが機能しない理由が混乱しています。私はここで答えを見ていた:How to use RSpec's should_raise with any kind of exception?と提案されたすべての組み合わせを試みたが、何らかの理由で、私はまだNoMethodErrorを得ている。rspec例外処理に続いて、 'expect'に '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 protected]", 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: "[email protected]", password: "foobar", password_confirmation: "foobar", admin: "true") 
ActiveModel::MassAssignmentSecurity::Error: 
    Can't mass-assign protected attributes: admin 

実行中:

RSpec 2.1.0 on Rails 3ガード・スポーク0.3.2とスポーク0.9.0

答えて

13

これは古典的です! itブロックがありません!

describe "admin should not be accesible" do 
    it "should bla" do 
    expect { User.new(name: "Example Name", email: "[email protected]", password: "foobar", password_confirmation: "foobar", admin: "true") }.should raise_error(ActiveModel::MassAssignmentSecurity::Error) 
    end 
end 
+6

この例では、 'expect {} .should'は非推奨です。' expect {} .to'を使用してください –

関連する問題