2016-11-17 50 views
1
class Post 
    def save 
    Mailer.notify!('bla') 
    true 
    end 
end 

如何測試當post.save被調用時,Mailer.notify方法被觸發?並有正確的論點。如何使用Minitest調用某個方法?

使用RSpec我通常做的:

expect(Mailer).to receive(:notify!).with('bla') 
post.save 

預先感謝您!

回答

3

你可以做這樣的事情:

describe 'Post#save' do 
    it "calls Mailer::notify!" do 
    mock = MiniTest::Mock.new 
    mock.expect(:call, nil, ['bla']) 

    Mailer.stub(:notify!, mock) do 
     post.save 
    end 

    mock.verify 
    end 
end 

是的,這是比較容易和RSpec的更直觀...