Rails Devise Mailer undefined method - ruby-on-rails

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!

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.

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.

NoMethodError: undefined method 'env' for nil:NilClass in ActionMailer::TestCase

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

Functional testing with Warden?

I'm trying to do functional testing and need to login with Warden.
I have done:
class ActionController::TestCase
include Warden::Test::Helpers
end
My test case is simple:
def test_access_admin_as_superuser
login_as(Person.make(:superuser))
get :index
assert_response :success
assert_nil flash[:warning]
end
I do a login_as(user) and it seems to work except when I do a get '/admin' it will fail with:
1) Error:
test_access_admin_as_superuser(AdminControllerTest):
NoMethodError: undefined method `user' for nil:NilClass
/home/silas/.rubygems/gems/gems/rails_warden-0.5.2/lib/rails_warden/controller_mixin.rb:21:in `current_user'
app/controllers/application_controller.rb:100:in `require_user'
test/functional/admin_controller_test.rb:20:in `test_access_admin_as_superuser'
any ideas?
Devise has helpers to deal with this, which is of little help if you're just using Warden. The reason things break is because action controller sets things up in the test environment in a different way. I put together a little gem that provides a module you can include for working with warden with rails in test mode: github.com/ajsharp/warden-rspec-rails.

Testing devise with shoulda

I'm having some difficulties in testing devise with shoulda:
2) Error:
test: handle :index logged as admin should redirect to Daily page.
(Admin::DailyClosesControllerTest):
NoMethodError: undefined method `env' for nil:NilClass
devise (1.0.6) [v] lib/devise/test_helpers.rb:52:in
`setup_controller_for_warden'
I have this in my test_helper:
include Devise::TestHelpers
Thoughts ?
Thanks in advance,
Cristi
include Devise::TestHelpers doesn't go in the test_helper.rb file, but rather inside the scope of the individual testing classes. Just like their README shows:
class ActionController::TestCase
include Devise::TestHelpers
end
I'm not sure if rspeicher is fully correct, but putting:
class ActionController::TestCase
include Devise::TestHelpers
end
at the very bottom of test_helper.rb (yes after the END of the class ActiveSupport::TestCase) should work. It has for 3 or 4 projects of mine so far, including one i'm working on today.
You then can use sign_in users(:one) if you are using fixtures, in your tests. Unless shoulda is messing it up?

Resources