繁体   English   中英

Mailer在Rails RSpec中将nil抛出undefined []

[英]Mailer throwing undefined [] for nil in rails rspec

我最近开始在我的rspec输出中看到以下错误消息:

undefined method [] for nil:NilClass

请注意,这不会使测试失败,它只会将此错误消息放入缓冲区。 我设法跟踪了由于邮件程序而发生的情况。 但是,我似乎找不到任何可能产生的东西,也没有找到回溯痕迹的任何方法。

不好意思,但是我想确保包括所有相关代码:

## app/models/invite.rb
class Invite < ActiveRecord::Base
  include Token

  belongs_to :account
  after_create :send_email
  def send_email
    InviteMailer.invite(self).deliver
  end
end

## app/mailers/invite_mailer.rb
class InviteMailer < ActionMailer::Base
  def invite(invite)
    @invite  = invite
    @account = invite.account

    mail to: invite.email, subject: "You're invited to help manage #{@account.name}"
  end
end

## spec/models/invite_spec.rb
require 'spec_helper'
describe Invite do
  let(:account){create(:account)}

  it 'mails the user' do
    account.invites.create(email: 'my@email.com')
  end
end

## config/environments/test.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :test

## Test Results
› be rspec spec/models/invite_spec.rb
undefined method `[]' for nil:NilClass
undefined method `[]' for nil:NilClass
.

Finished in 0.79803 seconds
1 example, 0 failures

Randomized with seed 46468

感谢您的帮助,这让我发疯了。

建议是在您的invite_spec.rb更改let

let!(:account){create(:account)}

让bang立刻创建它,而不是懒惰地创建它。 nil:NillClass error我收到nil:NillClass error我都会检查let。 它很容易被忽视。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM