Rails minitest syntax for get :index - ruby-on-rails

I am reading the Hartl tutorial and I have a question about whether or not this is the right syntax. How does rails know what controller to look in? I have 10 controllers and I am writing a test for static pages controller. How does it know what get :home refers to and where? Does it look in the associated controller with the file name? Or does it look in routes?
Also, is assert_response a rack test or a rails test?
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get :home
assert_response :success
end
test "should get help" do
get :help
assert_response :success
end
end

How does it know what get :home refers to and where?
Rails does a lot of magic, some of this magic falls into the category of convention over configuration.
In this case, the fact that the name of this test's class is StaticPagesControllerTest tells the test suite that it is testing the StaticPagesController.
It is standard rails convention to name tests this way. You could technically configure any test to look at any controller or action you want, but following this convention has many benefits (another discussion).
When you tell it to get :home it is using the get http method on the controller action named :home.
Also, is assert_response a rack test or a rails test?
It asking rails for the status code received. In this case it is checking to see if the status code was 200.
Additional info: http://api.rubyonrails.org/classes/ActionDispatch/Assertions/ResponseAssertions.html#method-i-assert_response

Related

Testing against engine routes in Rails with Test::Unit

I am trying to make some functional tests against new functionality in an application that uses Spree.
The problem that I am running into is that when I launch my created test, the path variables that should exists are not there for the test to execute.
Say I have these routes in my local routes.rb:
Spree::Core::Engine.routes.draw do
namespace :admin do
resources :image_games
end
end
Now in my test I would have the following:
require 'test_helper'
module Spree
module Admin
class ImageGamesControllerTest < ActionController::TestCase
test 'Should get index' do
get admin_image_games_path
assert_response :success
end
end
end
end
What happens is that a Minitest::UnexpectedError: NameError: undefined local variable or methodadmin_image_games_path'` error will be thrown.
I found some advice about this but all of the solutions seem to be for RSpec. I would like to keep using Test::Unit as I feel more at home with it.
So my question, how to load in routes from a foreign Rails Engine into functional tests?
EDIT:
It seems that just the helpers are not working for my tests.
Using get '/store/admin/image_games' works but get admin_image_games_path does not. When running the application normally then these helpers do work tough.

URI::InvalidURIError: bad URI(is not URI?) testing Rails controllers

I get URI::InvalidURIError testing Rails Home controller:
require 'test_helper'
class HomeControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get :index
assert_response :success
end
end
get the following error:
E
Error:
HomeControllerTest#test_should_get_index:
URI::InvalidURIError: bad URI(is not URI?): http://www.example.com:80index
test/controllers/home_controller_test.rb:7:in `block in <class:HomeControllerTest>'
The stack is the following:
Rails 5.0.0.beta3
minitest (5.8.4)
Controller tests inherit from ActionController::TestCase, while your test
inherits from ActionDispatch::IntegrationTest. So you're using an integration test and not a controller test.
The error is:
http://www.example.com:80index
That doesn't look right, does it? ;-)
The solution is to use a full path:
get '/index'
Remember, integration tests aren't really tied to any specific controller (or anything else, for that matter). They test the integration of several components in your application. So if you're testing the index action of a UserController you'd probably need to use /users/index.
If you intended to make a controller test and not an integration test, you want to set the correct superclass. Using get :index (for the index method) should work fine then.
You can try:
get home_index_path
instead of:
get :index

Rspec tests passing without actions being implemented

Hi there am working on a rails project, using a vagrant box(with ubuntu server fully configured with passenger, trying a hand at developing on a production like environment because this is my target production environment).
My problem though is that when i run "rails g controller people" and i write a simple controller spec like;
require 'spec_helper'
describe PeopleController do
describe "GET #index" do
it "responds with success" do
get :index
expect(response).to be_success
end
end
end
and i run it with "rspec spec/controllers/", the test passes right away even without having created an index action in the controller.
What do you think could be causing this?
Thank you
Is there an index.html.erb in app/views/people? If so Rails implicitly creates an action for you that renders the template.

Rails failing rspec

I am trying to check the title of a page, but it is failing with:
rspec ./spec/controllers/pages_controller_spec.rb:13 # PagesController GET 'home' should have the right title
Here is what pages_controller_spec.erb looks like:
render_views
describe "GET 'home'" do
it "returns http success" do
get 'home'
response.should be_success
end
it "should have the right title" do
get 'home'
response.should have_selector("title",:content => "Home")
end
end
According to this, I think you need :text not :content:
http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Finders#all-instance_method
I hope that helps.
rspec controller tests stub the view so you can test the controller in isolation
https://www.relishapp.com/rspec/rspec-rails/v/2-12-2/docs/controller-specs/views-are-stubbed-by-default
if you check the response object it probably contains a blank string
the idea being you should test
if the correct template rendered
if the controller redirected correctly
if the controller called the correct models, etc...
that being said, if you want the test to render the view, use render_views in the describe block
https://www.relishapp.com/rspec/rspec-rails/v/2-2/docs/controller-specs/render-views
Building off of what house9 says, you should test the title of your views somewhere else, and not in your controller specs. For instance, you can create a "requests" directory within your spec directory and test page features there. For more information on request specs:
http://railscasts.com/episodes/257-request-specs-and-capybara?view=asciicast

Using mocha for controller in functional test with RSPEC

I'm doing some tests here using Rspec and I would like to assure that the controller is calling the log method in some actions. I'm also using mocha.
I would like something like this:
it "update action should redirect when model is valid" do
Tag.any_instance.stubs(:valid?).returns(true)
put :update, :id => Tag.first
controller.expects(:add_team_log).at_least_once
response.should redirect_to(edit_admin_tag_url(assigns[:tag]))
end
is there something to use as the 'controller' variable? I tried self, the controller class name...
I just got helped with this. For testing controllers, you'd nest your specs inside a describe which names the controller. (The spec should also be in the Controllers folder)
describe ArticlesController do
integrate_views
describe "GET index" do
...
it "update action should redirect when model is valid" do
...
controller.expects(:add_team_log).at_least_once
...
end
end
end
I think you want #controller instead of controller. Here's an example from my test suite:
it "delegates to the pricing web service" do
isbn = "an_isbn"
#controller.expects(:lookup)
.with(isbn, anything)
.returns({asin: "an_asin"})
get :results, isbn: isbn
assert_response :success
end

Resources