Mailer throwing undefined [] for nil in rails rspec - ruby-on-rails

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.

Related

Controller Test Error expecting <"categories/new"> but rendering with <[]>

I am just started learning Ruby on Rails. Current chapter is about testing, while I do testing it is happening like this? What do i need to do? and What should I learn more to understand testing ruby on rails ?
Here's my Controller test code.
require 'test_helper'
class CategoriesControllerTest < ActionDispatch::IntegrationTest
def setup
#category = Category.create(name: "sports")
#user = User.create(username: "supyaeaung", email: "supyaeaung27714#gmail.com", password: "hlaing", admin: true)
end
test "should get new" do
sign_in_as(#user, "password")
get new_category_path
assert_response :success
end
Here's the error. What should I do ?
Error:
CategoriesControllerTest#test_should_get_new:
NoMethodError: undefined method `confirm' for #<User:0x00007fd0781e0370>
test/controllers/categories_controller_test.rb:15:in `block in <class:CategoriesControllerTest>'
Your controller, on line 15 tries to call method confirm on User object:
NoMethodError: undefined method `confirm' for #<User:0x00007fd0781e0370>
test/controllers/categories_controller_test.rb:15
You probably didn't implement it.

Rails Devise Mailer undefined method

I'm having issues now that i've added devise to my web app, theres a few SO questions on this but they just all point to a readme file and don't actually provide any fixes. I've had to include devise helpers as shown below in order to remove an issue saying the authentication method didn't exist
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
include Devise::TestHelpers
end
If i don't include it i end up with a huge amount of authentication errors:
ActionView::Template::Error: undefined method `authenticate' for nil:NilClass
but if it is included
NoMethodError: undefined method `env' for nil:NilClass
Simply occurs in different places instead, is there an actual fix?
Example
ContactMailerTest#test_should_return_contact_email:
NoMethodError: undefined method `env' for nil:NilClass
Method:
test "should return contact email" do
mail = ContactMailer.contact_email("Email#Email.com", "Example",
"SomeExample", #comment = "Hello")
assert_equal ['info#mynotes.com'], mail.to
assert_equal ['info#mynotes.com'], mail.from
end
This appears to be an issue with "Apparently there are issues with Devise::TestHelpers and integration testing" i am wondering if there is any known fix
Thanks!

NoMethodError: undefined method `env' for nil:NilClass

I am using rails testing for my app and when I run this test
class ShowingQuizzesTest < ActionDispatch::IntegrationTest
include Devise::TestHelpers
setup { host! 'example.com' }
test 'return quiz by id' do
instructor=Instructor.create(name:"same7", email:"abcdefg#eng.asu.edu.com", password:"123abc")
sign_in instructor
quiz = Quiz.create(name: 'Quiz1', subject: 'physics', duration: 10, no_of_MCQ: 5, no_of_rearrangeQ: 5)
current_instructor.quizzes << quiz
get "api/quizzes/#{quiz.id}"
assert_equal 200, response.status
quiz_response = json(response.body)
assert_equal quiz.name, quiz_response[:name]
end
end
This error appears:
1) Error:
ShowingQuizzesTest#test_return_quiz_by_id:
NoMethodError: undefined method `env' for nil:NilClass
the method I am testing:
def show
current_instructor.quizzes.find(params[:id])
render json: data:{:quiz => quiz}, status: 200
end
Are you on Rails 5?
It looks like Rails 5 deprecated the old method of including test helpers - as the Rails team has made significant improvements to the speed of integration tests. Currently, all controllers are tested in integration tests - Link
I'm getting the same error when dealing with devise & testing. I was able to make the error go away by commenting out # include Devise::Test::ControllerHelpers from my test file or the test_helpers.rb if you inherit your test helpers there.

Rspec controller test - should_receive on instance of model returns 0 times

I'm having an issue with Rails 4.0.3 and rspec 2.14.1 in testing a controller.
The relevant portion of the controller is:
class LoginsController < ApplicationController
def sign_in
#user = User.find_by(email: params[:email])
# ... - a few other codepaths but nothing that looks for primary_phone
if params[:email]
#user.send_token
flash[:notice] = "blah blah"
end
end
User.rb is:
class User < ActiveRecord::Base
# ...
def send_token
raise 'Tried to send token to contact with no phone number' if primary_phone.nil?
SMSSender.sms(primary_phone,"Your login code is: #{generate_token}")
end
end
The spec is:
require 'spec_helper'
describe LoginsController do
it "sends a token if a valid email is provided" do
#u = create(:user, primary_phone: "abc")
User.any_instance.should receive(:send_token)
post 'sign_in', email: #u.email
end
end
And, my user factory:
FactoryGirl.define do
factory :user do
name "MyString"
email "a#b.com"
end
end
When I change the spec's #u = create line to #u = create(:user) (ie, omitting the primary_phone), I get:
Failure/Error: post 'sign_in', email: #u.email
RuntimeError:
Tried to send token to contact with no phone number
# ./app/models/user.rb:16:in `send_token'
# ./app/controllers/logins_controller.rb:19:in `sign_in'
# ./spec/controllers/logins_controller_spec.rb:14:in `block (3 levels) in <top (required)>'
This is as expected. When I change it back to include the primary_phone, I get:
1) LoginsController sign_in sends a token if a valid email is provided
Failure/Error: User.any_instance.should receive(:send_token)
(#<RSpec::Mocks::AnyInstance::Recorder:0x007ff537ed4bd8>).send_token(any args)
expected: 1 time with any arguments
received: 0 times with any arguments
# ./spec/controllers/logins_controller_spec.rb:14:in `block (3 levels) in <top (required)>'
Having trouble understanding why that change would prevent the spec from passing. I did attach a debugger right after the 'post' in the spec and looked at the flash to see if it was correct (i.e., to ensure the proper code tree in the controller was being run) and it is.
The problem is you need to say should_receive rather than should receive. This is because of the any_instance. User.any_instance.should receive means that whatever object any_instance returns (an RSpec::Mocks::AnyInstance::Recorder) should receive the call. Of course that's not what you want, because that object is also not the same instance as what the controller instantiates. (In fact it's not even a User.) So the Recorder has a special should_receive method that does what you actually want. Tricky!
The User object you've created in your spec is not the same User object that the sign_in method creates and sends send_token to, so the expectations you set on #u as reflected in your error message are not going to be met. They both are associated with the same underlying database record, but they are different Ruby objects. (Note: In the first version of your question, the code you showed for your spec didn't match the error you showed, as the code showed setting an expectation on User.any_instance whereas your error message reflected setting an expectation on #u
Further, the expectations need to be set prior to the call you are expecting (e.g. prior to the post in your case, as noted in the comment by #PaulAJungwirth.
Finally, as an alternative to the answer provided by #PaulAJungwirth, you can use:
expect_any_instance_of(User).to receive(:send_token)
to address the problem with the your stated expectation line.

get method for testing in rails

I'm following along with RailsSpace: Building a Social Networking Website with Ruby on Rails by Michael Hartl. Running rails v2.3.2.
I've gotten to the 5th chapter in which testing is introduced. The following is supposed to match the title of the various pages to strings using the get method:
require File.dirname(__FILE__) + '/../test_helper'
require 'site_controller'
# Re-raise errors caught by the controller.
class SiteController; def rescue_action(e) raise e end; end
class SiteControllerTest < Test::Unit::TestCase
def setup
#controller = SiteController.new
#request = ActionController::TestRequest.new
#response = ActionController::TestResponse.new
end
def test_index
get :index
title = assigns(:title)
assert_equal "Welcome to RailsSpace!", title
assert_response :success
assert_template "index"
end
def test_about
get :title
title = assigns(:title)
assert_equal "About RailsSpace", title
assert_response :success
assert_template "about"
end
def test_help
get :help
title = assigns(:title)
assert_equal "RailsSpace Help", title
assert_response :success
assert_template "help"
end
end
On compiling I get:
Loaded suite site_controller_test
Started
EEE
Finished in 0.057 seconds.
1) Error:
test_about(SiteControllerTest):
NoMethodError: undefined method `get' for #<SiteControllerTest:0x4854b30>
site_controller_test.rb:23:in `test_about'
2) Error:
test_help(SiteControllerTest):
NoMethodError: undefined method `get' for #<SiteControllerTest:0x4854b1c>
site_controller_test.rb:31:in `test_help'
3) Error:
test_index(SiteControllerTest):
NoMethodError: undefined method `get' for #<SiteControllerTest:0x485470c>
site_controller_test.rb:15:in `test_index'
3 tests, 0 assertions, 0 failures, 3 errors
Other people have had this issue and the only proposed solution is just to reinstall. I'm not to enthused by this. Since this is an older book there this is probably just breakage between rails versions. What would be the equivalent of this for rails v2.3.2?
Replace all the following code
# Re-raise errors caught by the controller.
class SiteController; def rescue_action(e) raise e end; end
class SiteControllerTest < Test::Unit::TestCase
def setup
#controller = SiteController.new
#request = ActionController::TestRequest.new
#response = ActionController::TestResponse.new
end
with
class SiteControllerTest < ActionController::TestCase
The code you are using refers to Rails 2.0/2.1.
Try changing Test::Unit::TestCase to ActionController::TestCase.
One other thing you might like to know is that Railspace has evolved into Insoshi so the latest code is available there. Might be handy for you when you run into other issues.

Resources