NoMethodError: undefined method `env' for nil:NilClass - ruby-on-rails

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.

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

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