[英]undefined method `session' for nil:NilClass in RSpec Rails
[英]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.