0

I'm trying to integrate my application with the Sendgrid api. For example, when a user registers add the user's email to Sendgrid.

As of now I have the following tests:

context "painter registers" do
      let(:new_painter) {FactoryGirl.build(:painter)}
      it "#add_email_to_sendgrid is called" do
        new_painter.should_receive(:add_email_to_sendgrid)
        new_painter.save
      end
      it "Sendgrid#add_email called and should return true" do
        new_painter.email = "nobody-sendgrid@painterprofessions.com"
        Sendgrid.should_receive(:add_email).with("nobody-sendgrid@painterprofessions.com").and_return(true)
        new_painter.save
      end
    end

In my Painter model I have the following:

 after_create :add_email_to_sendgrid

def add_email_to_sendgrid
   Sendgrid.add_email(self.email)
  end

lib/sendgrid.rb

module Sendgrid
    def self.add_email(email_address)
      return false
    end
end

Everything is working up until the return value of Sendgrid.add_email.

I can change the add_email methods return value to anything and the test will still pass.

Please advise.

4

1 回答 1

0

我猜你的阅读方式存在混淆:

Sendgrid.should_receive(:foo).with(args).and_return(bar)

您实际上可以返回任何您想要的东西,这不是期望,这只是模拟服务的一种方便方式。

所以:

  • 是的,您可以退回任何您想要的东西。
  • 是的,您的规范将传递返回的任何值,因为您没有指定与之相关的任何内容

我猜你预计失败会以某种方式失败,因为after_create返回错误,如果是这样,创建依赖于外部服务有点尴尬。

于 2013-09-29T18:00:22.900 回答