Alright, a little embarrassed I asked a very similar question yesterday, but we're stuck again.
We've fixed all of our controller tests, and started writing integration tests. We're encountering errors on all of our integration tests, even the famous assert = true:
site_layout_test.rb
require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
test "the truth" do
assert true
end
#commenting out our real tests for debugging
=begin
test "top navigation bar links" do
get 'beta/index'
assert_template 'beta/index'
assert_select "a[href=?]", home_path
assert_select "a[href=?]", how_it_works_path
to do add map, community, sign up, login paths
to do: add paths to links on dropdown if user is logged in
end
=end
end
Terminal Test Results
12:31:32 - INFO - Running: test/integration/site_layout_test.rb
Started with run options --seed 27747
ERROR["test_the_truth", SiteLayoutTest, 2015-10-25 11:36:28 +0800]
test_the_truth#SiteLayoutTest (1445744188.33s)
NoMethodError: NoMethodError: undefined method `env' for nil:NilClass
1/1: [===================================] 100% Time: 00:00:00, Time: 00:00:00
Finished in 0.05380s
1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use!
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
include Devise::TestHelpers
# Add more helper methods to be used by all tests here...
#
def setup_variables
#base_title = "TripHappy"
end
end
Unfortunately, that error message gives us very little clue as to where the error is occurring. I tried reading up on Minitests, but without an idea of where to look I'm fairly lost.
Thank you in advanced!
For reference, we're following M. Harti's Rails Tutorial, which means we're using Guard and Minitest Reporters. We also have a login system via Devise, if that affects anything.
Solution:
It was an issue with the Devise. I included include Devise::TestHelpers in class ActiveSupport::TestCase instead of class ActionController::TestCase.
It was an issue with the Devise. I included include Devise::TestHelpers in class ActiveSupport::TestCase instead of class ActionController::TestCase
Related
I've made an application with the omniauth-facebook gem. All of my rails test were passing, but after adding in code to check for an authenticated user for access to certain actions - some of my tests fail. How can I go about adding code to my tests to log in a "test" user so that I can simulate an authenticated user? Most of the google results showed examples for Rspec or Devise examples, but I've been using the default testing suite for Rails 5, so it's been difficult for me to find some examples of how to properly do this. Please let me know if you'd like me to provide any specific samples of my current tests. Nothing relevant came to mind to include since I'm not sure where to start with this.
So after reading through documentation here, and trying various things here's what I came up with...
test/test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:facebook, {:uid => '123456', info: { email: 'email_address_of_user_in_fixtures#gmail.com' }})
def sign_in_admin
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
get auth_facebook_callback_path
end
end
test/controllers/restaurants_controller_test.rb
require 'test_helper'
class RestaurantsControllerTest < ActionDispatch::IntegrationTest
setup do
# I sign in an admin user, and proceed
sign_in_admin
#restaurant = restaurants(:mcdonalds)
end
# other test methods for actions ...
end
config/routes.rb
Rails.application.routes.draw do
#...
get 'auth/facebook/callback', to: 'sessions#create'
#...
end
I've upgraded to Rails 5. My first hurdle in getting specs to pass is a 'No route matches' error.
Please see my test and test_helper below. Is there anything I need to add to test_helper or test.rb? Anyone know the cause or how to resolve this?
.....
I've been running a single test while trying to simply get a pass:
bin/rails test test/controllers/users_controller_test.rb:31
which is the 'should get new' line in my users_controller_test.rb
require 'test_helper'
describe UsersController do
//class UsersControllerTest < ActionDispatch::IntegrationTest
before do
glenn = users(:glenn)
sign_in(glenn)
end
it 'should get new' do
get new_user_url
value(response).must_be :success?
end
end
this results in the following error.
Error:
UsersController#test_0002_should get new:
ActionController::UrlGenerationError: No route matches {:action=>"http://test.host/users/new", :controller=>"users"}
test/controllers/users_controller_test.rb:32:in `block (2 levels) in <top (required)>'
test_helper.rb
ENV['RAILS_ENV'] = 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'minitest/rails'
class ActionController::TestCase
include ActiveJob::TestHelper
include Devise::Test::ControllerHelpers
end
class ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
end
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
fixtures :all
include ActionDispatch::TestProcess # fixture_file_upload
end
I had this same issue and was able to get it to work, though I'm not entirely sure about the internals of why it works.
Apparently the type of spec is necessary.
What I had that resulted in similar error as above:
require 'rails_helper'
RSpec.describe TimingsController, type: :controller do
describe "GET new" do
it "renders the new template" do
get new_timing_path
expect(response).to be_successful
end
end
end
What I have now that works:
require 'rails_helper'
RSpec.describe TimingsController, type: :controller do
describe "GET new", type: :request do
it "renders the new template" do
get new_timing_path
expect(response).to be_successful
end
end
end
So it seems like the type: request part is essential.
While troube-shooting this I created a new rails 5 app, installed devise and minitest-rails...and those tests are passing. The new url style syntax is working fine inside the describe block in my controller tests. However, in the app that pertains to this questions...the url syntax is not working inside the describe block and the fix---at least for now, was to replace the describe block with a class that inherits from ActionDispatch::IntegrationTest. I have no idea, yet, why this is.
Does setting host help for your case?
http://api.rubyonrails.org/classes/ActionDispatch/Integration/Session.html#method-i-host
host! "subdomain.example.com"
After upgrading my rails app to 4.2, I started writing automated tests.
I started by doing something super simple:
class SimpleTest < ActionDispatch::IntegrationTest
test 'Browse a page' do
assert true
get '/'
assert_response :success
end
end
But when I:
▶ rake test
▶ bin/rake test
Nothing happens. No error, no blocking, process finishes, just nothing.
Did I miss something?
I was missing the require 'test_helper' at the beginning of my test, containing:
# /test/test_helper.rb
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
# Add more helper methods to be used by all tests here...
end
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?
I'm writing a plugin that adds a class method to ActionController::Base, so in my functional test I am creating a simple controller to test against. The example below is totally stripped down to basically do nothing. The test succeeds all the way up to assert_template, which fails.
require 'rubygems'
require 'test/unit'
require 'active_support'
require 'active_support/test_case'
require 'action_controller'
require 'action_controller/test_process'
class UnderTestController < ActionController::Base
def index; end
end
ActionController::Routing::Routes.draw {|map| map.resources :under_test }
class MyPluginTest < ActionController::TestCase
def setup
#controller = UnderTestController.new
#request = ActionController::TestRequest.new
#response = ActionController::TestResponse.new
end
test "should render the correct template" do
get :index
assert_template :index # everything works except this assert
end
end
Here are the results from running the above file:
Loaded suite /Users/jjulian/sandbox/vendor/plugins/my_plugin/test/my_plugin_test
Started
E
Finished in 0.014567 seconds.
1) Error:
test_should_render_the_correct_template(MyPluginTest):
NoMethodError: undefined method `[]' for nil:NilClass
method assert_template in response_assertions.rb at line 103
method clean_backtrace in test_case.rb at line 114
method assert_template in response_assertions.rb at line 100
method test_should_render_the_correct_template in so_test.rb at line 22
method __send__ in setup_and_teardown.rb at line 62
method run in setup_and_teardown.rb at line 62
1 tests, 0 assertions, 0 failures, 1 errors
My questions: what am I missing to allow assert_template to work? Am I requiring the correct libraries? Extending the correct TestCase class?
I figured it out. First, the missing template is because the view_path needs to be explicitly set in the controller under test:
before_filter {|c| c.prepend_view_path(File.join(File.dirname(__FILE__), 'fixtures')) }
I created a dummy erb file in test/fixtures/under_test/index.erb. That brings me back to the nil error in response_assertions.rb. Well, it looks like I need to include another test class:
require 'action_view/test_case'
Now it works.
One additional note: assert_template uses match to determine success, so in this case, assert_template 'ind' would succeed, as would assert_template 'x'.
Try replacing the setup method in the test with just tests UnderTestController. Rails takes care of setting up the test controller, request and response for you automatically.