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
Related
I'm trying to run a rake test using devise (using a user that's been created by database seeds)
Running rake test produces the following error:
Failure:
CustomersControllerTest#test_should_get_index [/home/ubuntu/workspace/test/controllers/customers_controller_test.rb:7]:
Expected response to be a <2XX: success>, but was a <302: Found> redirect to <http://www.example.com/users/sign_in>
Response body: <html><body>You are being redirected.</body></html>
My test looks like this:
test "should get index" do
get customers_url
assert_response :success
end
It's a very simple test, but I don't know how to actually login to the site before performing the test.
To test actions that require an authenticated user you can use Devise's sign_in and sign_out helpers. These come from Devise::Test::ControllerHelpers on your test case or its parent class (or Devise::Test::IntegrationHelpers for Rails 5+):
class CustomersControllerTest < ActionController::TestCase
include Devise::Test::ControllerHelpers # <-- Include helpers
test "should get index" do
sign_in User.create(...) # <-- Create and authenticate a user
get customers_url
assert_response :success
end
end
See the controller tests section in the Devise README for details.
I'm using Rails 5 and minutest and trying to write a test for a controller. The test is
# issues_controller_test.rb
class IssuesControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
test "logged in should get issues page" do
sign_in users(:one)
test_stop_id = 1
test_line_id = 1
get new_issue, :stop_id => test_stop_id, :line_id => test_line_id
assert_equal test_stop_id, #issue.stop_id
assert_equal test_line_id, #issue.line_id
assert_response :success
end
end
The method in question attempts to access this page, defined in my rails routes ...
new_issue GET /issues/new(.:format) issues#new
Yet when I run the test, I get this error
# Running:
.E
Error:
IssuesControllerTest#test_logged_in_should_get_issues_page:
NameError: undefined local variable or method `new_issue' for #<IssuesControllerTest:0x007f816759b330>
test/controllers/issues_controller_test.rb:10:in `block in <class:IssuesControllerTest>'
bin/rails test test/controllers/issues_controller_test.rb:6
Finished in 0.103956s, 19.2389 runs/s, 9.6195 assertions/s.
Why is the test framework unable to find a method defined in my routes file?
new_issue that way is just a local variable defined nowhere, try specifying it as a working url with new_issue_url:
test 'logged in should get issues page' do
sign_in users :one
test_stop_id = 1
test_line_id = 1
get new_issue_url, stop_id: test_stop_id, line_id: test_line_id
assert_equal test_stop_id, #issue.stop_id
assert_equal test_line_id, #issue.line_id
assert_response :success
end
In the course I have been doing,I was given a homework assignment to make an integration test of the users' signup page.Here is my work:
require 'test_helper'
class SignupTest < ActionDispatch::IntegrationTest
test "create a new user" do
get '/signup'
assert_template '/signup'
assert_difference 'User.count',1 do
post :create,user:{username: "jay",email:"jay_lann#gmail.com",password: "american"}
assert_redirected_to user_path(assigns(:user))
end
assert_template "users/#{:user.id}"
end
end
However,an error pops out when I rake test the file.It throws out this error:
Failure:
SignupTest#test_create_a_new_user [c:/Users/dadi/Desktop/project1/app/test/integration/signup_test.rb:7]:
expecting <"/signup"> but rendering with <["shared/_errors", "users/_form", "users/new", "layouts/_navigation", "layouts/_messages", "layouts/_footer", "layouts/application"]>
I've seen some solutions for this error,but none of them have worked.Help is greatly appreciated.Thanks in advance!
(Since I am not well-versed yet in testing,pointing out any error I have made but hasn't popped out yet would mean much to me.)
EDIT:I have tried your solutions.My code looks like this now:
require 'test_helper'
class SignupTest < ActionDispatch::IntegrationTest
test "create a new user" do
get '/signup'
assert_difference 'User.count',1 do
post :create,user:{username:"jay",email:"jay_lann#gmail.com",password: "american"}
assert_redirected_to user_path(assigns(:user))
end
assert_template "users/#{:user.id}"
end
end
However,I receive this error when I rake test the file:
Error:
SignupTest#test_create_a_new_user:
URI::InvalidURIError: bad URI(is not URI?): http://www.example.com:80create
test/integration/signup_test.rb:9:in `block (2 levels) in <class:SignupTest>
'
test/integration/signup_test.rb:8:in `block in <class:SignupTest>'
The error is in lines 8 and 9,but I can't figure out what's wrong(invalid URI code,I know,but I can't figure out where did I make an URI error).
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"
I have application without tests, now I'm trying to add them (minitest-rails), but something wrong.
I'm typing rails generate controller test test
and I have:
require "minitest_helper"
class TestControllerTest < MiniTest::Rails::ActionController::TestCase
test "should get test" do
get :test
assert_response :success
end
end
now I'm typing rake test and Expected response to be a <success>, but was <301> error occuring.
Where is the problem?
Edited:
My controller's code:
class TestController < ApplicationController
def test
end
end
I've solved it! Just deleted force_ssl and added config.force_ssl = true in production config.