简体   繁体   中英

Mailer throwing undefined [] for nil in rails rspec

I have recently started seeing the following error message among my rspec output:

undefined method [] for nil:NilClass

Note this does not make the test fail, it just puts this error message to the buffer. I've managed to track down that it is happening due to the mailer. However, I can't seem to find anywhere it might be produced, or any way to see a backtrace.

Excuse the large paste, but I wanted to make sure to include all the relevant code:

## 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

Thanks for any help, this one has been driving me crazy.

A suggestion would be to change the let in your invite_spec.rb

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

let bang creates it instantly rather than lazily. Anytime I get a nil:NillClass error I check the let. Its easy to overlook.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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