2012-07-06 23 views
5

के लिए 'NoMethodError' प्राप्त करने के कारण यह आरएसपीईसी विधि क्यों काम नहीं कर रही है। मैं यहां जवाब देख रहा था: 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 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 

मैं दौड़ रहा हूं:

आरएसपीसी 2.1.0 रेल-स्पा पर गार्ड-स्पार्क 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

इस उदाहरण में, 'उम्मीद {}। Should' को हटा दिया गया है, 'उम्मीद {} .to' का उपयोग करें –

संबंधित मुद्दे