문제

I'm trying to write a test for my Message model. This model has a method 'send_message':

def send_message
   ContactMailer.contact_mail(self.name, self.email, self.text, self.ip).deliver
end

In my rspec file I have the following:

mailer = double(ContactMailer)
mailer.should_receive(:contact_mail)

FactoryGirl.build(:message).send_message

I am receiving the following error:

 Failure/Error: mailer.should_receive(:contact_mail)
   (Double ContactMailer).contact_mail(any args)
       expected: 1 time with any arguments
       received: 0 times with any arguments

Any idea? Could it be because I'm not taking .deliver into account?

도움이 되었습니까?

해결책

No, the issue is that you haven't told RSpec that ContactMailer should receive that message. The argument to double is just a way to "name" the double for documentation purposes, as discussed in https://github.com/rspec/rspec-mocks

You actually don't need a double in this case, as you can set the expectation directly on the ContactMailer class as follows:

ContactMailer.should_receive(:contact_mail)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top