2012-07-06 11 views
5

Bu RSpec yönteminin neden çalışmadığı konusunda karışık. Buradaki cevaba bakıyordum: How to use RSpec's should_raise with any kind of exception? ve önerilen tüm kombinasyonları denedim ama bir nedenden dolayı hala bir NoMethodError alıyorum.rspec kural dışı durumlarını izleyerek, 'beklemek' için 'NoMethodError' almak

: bu yüzden benim yöntem bunu yapmak istediğinizi yaptığını biliyoruz

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 

Daha önce bu hata var: Burada

yöntem istisna İşte

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

olduğu

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 

Çalışıyorum:

RSpec 2.1.0, Rails 3, guard-spork 0.3.2 ve spork ile 0.9.0

cevap

13

Bu bir klasik! it bloğunu kaçırıyorsunuz!

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

Bu örnekte, 'beklemede {} .should' kullanımdan kaldırılmıştır, 'bekliyoruz {} .to'yu kullanın –

İlgili konular