2012-06-30 48 views
2

在Rails項目中做一些rspec工作。有點好奇,爲什麼這些不應成爲期望。有什麼想法嗎?Rspec:當我預計失敗時,nil.should_not be_nil會通過。爲什麼?

it "nils should be nil" do 
    @io = nil 
    @io.should_not be_nil #passes (shouldn't!!) 
    nil.should_not be_nil #passes (shouldn't!!) 
    @io.should == nil # passes 
    @io.should be_nil # passes 
    @io.nil?.should be_true # passes 
    #@io.nil?.should be_false # fails as expected 
end 

UPDATE:意見的基礎上在這裏,我已經能夠分離出這種情況的原因(這是從spec_helper加載的東西)。我相信這是由於.nil?.blank?可憐的混合,我將與開發人員談一談,看看這些重寫是否真的有必要。

感謝那些幫助驗證我的東西。

+0

那麼這不是什麼https://www.relishapp.com/rspec/rspec-expectations/v/2-10/docs/built-in-matchers/be-matchers說應該發生 –

回答

3

我無法重現您的陳述。我在一個像這樣的現有項目中使用了僞造的規範來編寫它們。

require 'spec_helper' 

describe "Pepper" do 
    describe "simplicity" do 
    before(:each) { @io = nil } 
    # should fail 
    it 'should be nil 1' do 
    @io.should_not be_nil 
    end 
    # should fail 
     it 'should be nil 2' do 
    nil.should_not be_nil 
    end 
    # should NOT fail 
    it 'should be nil 3' do 
    @io.should == nil 
    end 
    # should NOT fail 
    it 'should be nil 4' do 
    @io.should be_nil 
    end 
    # should NOT fail 
    it 'should be nil 5' do 
    @io.nil?.should be_true # passes 
    end 
    # should fail 
    it 'should be nil 6' do 
    @io.nil?.should be_false 
    end 
    end 
end 

返回 - 預期 - 以下的輸出:

simplicity 
should be nil 1 (FAILED - 1) 
should be nil 2 (FAILED - 2) 
should be nil 3 
should be nil 4 
should be nil 5 
should be nil 6 (FAILED - 3) 

因此,也許你spec_helper是暗示的東西,那就是刺激你的規格。

+1

是的。我試了一下,可以驗證結果。除非你定義了自己的函數be_nil,其結果不太可預測,否則'should_not be_nil'和'should be_n''不可能給出相同的結果。 'be_nil'只是在rspec中調用'nil?'。 – nathanvda

+0

感謝您的幫助。是的,我會再看看它,看看有什麼。我認爲我們不會覆蓋任何東西,但也許我們使用的是第三方代碼。 – zorn

相關問題