Rails testing template render error - ruby-on-rails

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

Related

Why is my test framework unable to find a URL defined in my routes file?

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

rspec can't find #user when application can

The following rspec test file
require 'spec_helper'
describe EvaluationsController do
render_views
before(:each) do
association_attr
end
describe "'eval_selektor'" do
before(:each) do
#eval_selektor = get :eval_selektor, student_group_id: #student_group
end
it "should be successful" do
#eval_selektor
response.should be_success
end
...
end
...
end
is throwing the following error:
1) EvaluationsController 'eval_selektor' should be successful
Failure/Error: #eval_selektor = get :eval_selektor, student_group_id: #student_group
NoMethodError:
undefined method `student_groups' for nil:NilClass
# ./app/controllers/application_controller.rb:7:in `get_student_group'
# ./spec/controllers/evaluations_controller_spec.rb:14:in `block (3 levels) in <top (required)>'
from this method in the application_controller:
def get_student_group
#user = current_user
#student_group = #user.student_groups.find(params[:student_group_id])
end
At first I thought maybe rspec just wasn't getting passed the method from application_controller, but that's not the case as it can see it in the error. The code works in the browser, and if I put <%= #user %> in the view, it shows the correct user instance. Any ideas why rspec can't read #user?
apneadiving got me started in the right direction, and between this post and this one I got to the correct code:
ApplicationController.any_instance.stub(:current_user).and_return(#user)

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

'visit *_path' doesn't work while testing user model

I've searched around and this seems like a common enough issue, but nothing has worked for me. Here goes!
Firstly, I'm working through the Hartl RoR tutorial. I'm towards the end of section 9 (video 905) and I'm trying to test a failing user login. Here's the code in user_spec.rb:
describe "failure" do
it "should not log the user in" do
visit login_path
puts response.body
fill_in 'session_email', with: ""
fill_in 'session_password', with: ""
click_button
response.should have_selector('div.login_error', content: "Invalid")
response.should render_template('sessions/new')
end
end
and here's the autotest error:
1) User login failure should not log the user in
Failure/Error: visit login_path
NameError:
undefined local variable or method `login_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_5::Nested_1:0x00000102c79248>
# ./spec/models/user_spec.rb:176:in `block (4 levels) in <top (required)>'
I tried writing visit login_path as visit '/login', and still got an error, although it changed to this:
Failure/Error: visit '/login'
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_5::Nested_1:0x00000102c2d140>
Which makes me think this is maybe a webrat issue, as I read somewhere that 'visit' comes from webrat (and not rspec). I also know that it's not getting past the first line, as the puts response.body isn't showing up on the autotest failure. More confusing is that in another part of my app, I have other test code that's virtually identical and it works fine.
layout_links_spec.rb:
before(:each) do
#user = Factory(:user)
visit login_path
# puts response.body
fill_in 'session_email', with: #user.email
fill_in 'session_password', with: #user.password
click_button
end
Any ideas?
I had to do a lot of updates to get your code running. I haven't done Rails on my home machine for a while so it was good to get it all up to date.
Found a solution to getting named routes to work but that just led to another problem. Finally the light went on. You are doing the test in the wrong place. The user model spec is not where you test login. As you mentioned, another similar test works. That is in the Request specs. The visit command and named routes are designed to work in that directory.

No route matches using rspec on a get request

What am I forgetting?
routes:
get "/comingsoon" => "visitors#comingsoon"
resources :visitors
controller:
class VisitorsController < ApplicationController
def comingsoon
#new_visitor = Visitor.new
end
end
spec:
require 'spec_helper'
describe VisitorsController do
describe "GET /comingsoon" do
it "should be happy" do
get "/comingsoon"
response.should be_success
end
end
end
And here's the result:
✗ rspec spec/controllers/visitors_controller_spec.rb
F
Failures:
1) VisitorsController GET /comingsoon should be valid
Failure/Error: get "/comingsoon"
ActionController::RoutingError:
No route matches {:controller=>"visitors", :action=>"/comingsoon"}
# ./spec/controllers/visitors_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
Finished in 0.14226 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/controllers/visitors_controller_spec.rb:6 # VisitorsController GET /comingsoon should be valid
What am I forgetting?
In your spec file replace get "/comingsoon"
with get "comingsoon"
When you spec a controller with rspec the operand of the http verb (get, post, put, delete) is an action of the controller rather than a url.
Possibly daft suggestion, but you have a view right? Otherwise you have to tell your controller to render something.

Resources