2012-06-21 88 views
0

好像我想要做的是大多數人要求的測試軌道時相反...如何測試質量分配錯誤

如何測試::加載ActiveModel :: MassAssignmentSecurity錯誤。我已經知道了,只是想知道如何讓測試用例變成綠色而不是紅色。

電流測試:

describe Tag do 
    let(:tag) { FactoryGirl.create(:tag) } 
    subject { tag } 

    describe "when tag name is sent directly" do 
    before { tag = Tag.new(name: "New") } # <- what I want to raise the error 
    it { should_not be_valid } # <- doesn't make it to this line 
    end 
end 

我應該如何構建這是一個合適的測試?我曾嘗試:

lambda do 
     tag = Tag.new(name: "NEW") 
    end.should raise_error 

但是,這並不對我也上班,我得到

Exception encountered: #<NoMethodError: undefined method `raise_error' for #<Class:0x00000102525c08>> 

回答

0

爾加,不得不把它的它{}內:

it do 
    lambda { tag = Tag.new(name: "NEW") }.should raise_error 
end 

這爲我解決了這個問題