开发者

How should I test class objects inside mailer files?

I am using Ruby on Rails 3.1.0, rspec-rails 2 and DelajdJob gems. In order to test if an e-mail is sent when a user signed up I am trying to test a delayed email but I have some trouble\doubt (not directly related to the DelayedJob gem).

In my controller I have:

def create
  ...
  Users::Mailer.delay.sign_up(@user)
  ...
end

In my mailer file I have:

class Users::Mailer
  def sign_up(user)
    @user = user

    # Note: 'authentication' refers to an associated model
    @authentication = user.authentication.user_token
    ...
  end
end

In my spec fi开发者_运维知识库le I have:

describe "POST create" do
  it "sends e-mail" do
    post :create, { :email => 'foo@bar.com' }

    # Here I would like to test
    #  (1) if the e-mail is send
    #  (2) if the e-mail is send to the 'foo@bar.com' address
  end
end

If I run the spec I get the following error:

Failure/Error: post :create,
NoMethodError:
  undefined method `user_token' for nil:NilClass

How can I solve that and test if the e-mail was sent? Should I mock or stub the user class object instance? What do you advice to make in my case?


def create
    ... # whatever is going on here is not setting @user.authentication
    Users::Mailer.delay.sign_up(@user)
    ...
end

Edit: I realized that I answered the problem that you were having with getting an error. As for mocking the user object, I would lean against using a mock in this case. What is important to test in this case is that an email was sent and it went to foo@bar.com. I would instead post what you need to create a user and then verify.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜