2011-12-25 43 views
1

測試模塊Rspec的should_receive的模塊不能正常工作,擴展自

module Notifications 
    extend self 

    def notify(push_id, platform, message, event, args) 
    puts "hello" 
    ...  
    end 
end 


Notifications.should_receive(:notify) 

Rspec的說,通知是不叫,但「你好」被打印到日誌中。

+2

您沒有包含應該引起此調用的代碼。我想如果這個模塊被包含在一個類中,那麼在該類的一個實例上調用'#notify',那麼應該打印出「hello」,但Notifications絕不會是消息的接收者。 –

回答

0

它正常工作對我來說:

module Notifications 
    extend self 

    def notify(push_id, platform, message, event, args) 
    puts "hello" 
    end 
end 


describe 'RSpec stubbing' do 
    it "works when I don't stub" do 
    Notifications.should_not_receive(:notify) 
    end 

    specify 'works when I do stub' do 
    Notifications.should_receive(:notify) 
    Notifications.notify(1,2,3,4,5) 
    end 
end 

你的代碼顯然是錯誤的,但是,因爲當你做#should_receive它不會調用原始的方法。如果你看到「hello」被打印出來,那麼它不會被RSpec抓住。可能你對你是否存在實例或模塊本身感到困惑。

+0

這只是代碼示例。我在內部規範中調用應該在其中調用Notifications.notify的代碼。 –

+0

我已經聲明應該在調用調用方法之前收到它並開始工作。 –

相關問題