2013-06-20 5 views
9

मैं साधारण परीक्षण मामला है:और परीक्षण में विफल रहता है, क्योंकि यह इस त्रुटि को जन्म देती है

it "is removed when associated board is deleted" do 
    link = FactoryGirl.create(:link) 
    link.board.destroy 
    expect(Link.find(link.id)).to raise_error ActiveRecord::RecordNotFound 
end 

और यह उत्पादन के साथ विफल रहता है:

1) Link is removed when associated board is deleted 
    Failure/Error: expect(Link.find(link.id)).to raise_error ActiveRecord::RecordNotFound 
    ActiveRecord::RecordNotFound: 
    Couldn't find Link with id=1 
    # ./spec/models/link_spec.rb:47:in `block (2 levels) in <top (required)>' 

किसी भी विचार क्यों?

उत्तर

20

त्रुटि पकड़ने के लिए आपको ब्लॉक में कोड लपेटने की आवश्यकता है। आपका कोड Link.find(link.id) को उस दायरे में निष्पादित कर रहा है जो त्रुटि की अपेक्षा नहीं कर रहा है। उचित परीक्षण:

it "is removed when associated board is deleted" do 
    link = FactoryGirl.create(:link) 
    link.board.destroy 
    expect { Link.find(link.id) }.to raise_error ActiveRecord::RecordNotFound 
end 
संबंधित मुद्दे