2012-07-06 24 views
5

Bối rối là tại sao phương pháp RSpec này không hoạt động. Tôi đã nhìn vào câu trả lời ở đây: How to use RSpec's should_raise with any kind of exception? và đã thử tất cả các kết hợp được đề xuất nhưng vì một lý do nào đó, tôi vẫn nhận được một NoMethodError.Sau khi xử lý ngoại lệ rspec, nhận 'NoMethodError' cho 'mong đợi'

Dưới đây là ngoại lệ

Exception encountered: #<NoMethodError: undefined method `expect' for #<Class:0x007fa5bd8e4120>> 

Dưới đây là phương pháp:

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 

Tôi đã nhận lỗi này trước đó vì vậy tôi biết rằng phương pháp của tôi là làm những gì tôi muốn nó phải làm:

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 

Tôi đang chạy:

RSpec 2.1.0 trên Rails 3 với spork guard 0.3.2 và spork 0.9.0

Trả lời

13

đây là một cổ điển! bạn đang thiếu khối 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

Trong ví dụ này, 'expect {} .should' đã không được dùng nữa, sử dụng' expect {} .to' –