NoMethodError: undefined method 'env' for nil:NilClass in ActionMailer::TestCase - ruby-on-rails

I'm starting in ruby on rails and created a sample application. In this application I have a mail class and a test generated Mail class.
I managed to get all tests working except the tests from ActionMailer::TestCase.
Every time I run the tests I get an error message:
NoMethodError: undefined method 'env' for nil:NilClass.
My generated mailer test method looks like this:
test "new_project" do
mail = ProjectMailer.new_project
assert_equal "New project", mail.subject
assert_equal ["to#example.org"], mail.to
assert_equal ["from#example.com"], mail.from
assert_match "Hi", mail.body.encoded
end
I have in my test_helper
class ActionController::TestCase
include Devise::TestHelpers
end
class ActionMailer::TestCase
include Devise::TestHelpers
end
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
include Devise::TestHelpers
fixtures :all
end
Anyone does know how to solve that?

You're including Devise::TestHelpers.
You should create specific test_helper for tests, that really need Devise.
Same question on stackoverflow.
undefined method 'env' for nil:NilClass

Related

Error when trying to include Devise::Test::ControllerHelpers

I'm trying to get started with controller tests, and I'm not sure what I'm doing wrong. Here's my code, and the error it's producing:
require 'test_helper'
class InvProcure::UserImportsControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::ControllerHelpers
test "should get index" do
user = users(:foobars_admin)
sign_in(:user, user)
get inv_procure_user_imports_path
assert_response :success
end
end
NoMethodError: undefined method `env' for nil:NilClass
/home/blaine/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/devise-4.7.1/lib/devise/test/controller_helpers.rb:42:in `setup_controller_for_warden'
It looks like the error might be happening when including devise test helpers, or when calling sign_in.
From the doc: https://www.rubydoc.info/gems/devise/Devise/Test/ControllerHelpers
Devise::Test::ControllerHelpers provides a facility to test controllers in isolation when using ActionController::TestCase allowing you to quickly sign_in or sign_out a user. Do not use Devise::Test::ControllerHelpers in integration tests.
You are inheriting the integration test class instead of the controller test class which is the default for controller tests now (From Rails 5 the controller tests are generated with the parent class as ActionDispatch::IntegrationTest instead of ActionController::TestCase). Devise::Test::ControllerHelpers was built for ActionController::TestCase and not for integration tests.
You can instead try to use Devise::Test::IntegrationHelpers which should have similar methods for integration tests.
Document: https://www.rubydoc.info/gems/devise/Devise/Test/IntegrationHelpers

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.

functionality testing of controller in ruby on rails

I have product controller in that i have method say homepage and _homepage.html.erb file in views. How to test this method to get assert should get response success. I created productcontrllertest.rb in test/functional and wrote below code
require 'test_helper'
class ProductControllerTest < ActionController::TestCase
test "should get homepage" do
get :homepage
assert_response :success
end
end
and i have routes.rb file with get "product/homepage"
when i run test with command bundle exec rake test:functionals i am getting error.
Error is that NoMethodError: undefined method `authenticate' for nil:NilClass
Please help me to fix this error.
require 'test_helper'
class ProductControllerTest < ActionController::TestCase
test "should get homepage" do
get 'homepage/1'
assert_response :success
end
end

Resources