Rails failing rspec - ruby-on-rails

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

Related

How is render_views implemented in RSpec (is it a variable, method)?

Learning RSpec 3+, and learning that in order to render the template from the controller, you have to write render_views within a example group like so:
describe CustomersController do
describe "GET INDEX" do
render_views
it "renders the index template"
expect(response.body).to match(/Customer List/)
end
end
end
Just wondering how the implementation of it, more specifically, how does it work behind the scenes. Is render_view a method, a variable? Just wondering how RSpec knows to render the views for that particular example group, if render_views is typed in?
You can see for yourself in the rspec-rails source code.

Testing page redirect in rails

How can I test this in rails:
A student goes to a list of their groups, and when they click the link, it brings them to that group page.
I have the gist of it down, but how can I test that it went to a page? I was thinking something along the lines of:
expect(page).to eq()
but then I am not sure what to do from there. I don't want to just expect the page to have the name of the group, because that that would be on the overall list and the page the link would bring them to. Any advice?
You're looking for the redirect_to() method. So an example would be:
require 'spec_helper'
describe FoobarController do
describe "GET foo" do
it "redirects to bar" do
get :foo
expect(response).to redirect_to(bar_path)
end
end
end

How to test if a page is rendered from rspec

I'm testing access to a page with a bunch of different types of users. I'm tyring to see which page is rendered for each type. I can't find the right syntax to do so. I'm also not sure if this should be in a views file rather than controller.
describe "GET show as admin" do
before(:each) do
session[:admin_user_id] = FactoryGirl.create(:admin_user).id
end
it "is available to view by admin" do
data_service = FactoryGirl.create(:published_data_service)
get :show, {:id => data_service.to_param}
assigns(:data_service).should eq(data_service)
response.should render_template('show')
end
end
render_template is returning as [].
Other note: Capybara is included in the Gemfile, and is being required in spec_helper

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

Rspec test for the existence of an action is not working

i am working in Rspec of ROR..
I am trying to test my controllers using RSpec.i am having a Users controller with functions like new , tags, etc..
i created a file under spec/users_controller_spec.rb
and added the test cases as.
require 'spec_helper'
describe UsersController do
integrate_views
it "should use UsersController" do
controller.should be_an_instance_of(UsersController)
end
describe "GET 'new'" do
it "should be successful" do
get 'new'
response.should be_success
end
it "should have the title" do
get 'new'
response.should have_tag("title", "First app" )
end
end
end
which gets pass.
But when i add a test case for tags ..
like
describe "GET 'tags'" do
it "should be successful" do
get 'tags'
response.should be_success
end
end
this results in an error as
F...
1)
'UsersController GET 'tags' should be successful' FAILED
expected success? to return true, got false
why it is coming like this ?? i am very new to ROR and cant find the reason of why i am getting this error..
How to make this pass .
Also i tried the Url
http://localhost:3000/users/tags which is running for me .. But on testing using $spec spec/ i am getting the error ..
Your test may be failing for any number of reasons. Does the route require an ID in the parameter hash? Is the controller action redirecting? Is the controller raising an error?
You'll need to look at the controller code /and/or routes.rb to discover the cause of the failure. Take note of before filters in the controller, which may not allow the action to be reachable at all.
You need to add custom routes that are not within the default 7 routes. Assuming you have resources :users within your routes you will need to modify it. I'm also assuming that your tags route is unique to individual users.
resources :users do
member do
# creates /users/:user_id/tags
get :tags
end
end
And in your RSpec test you would call it like
describe '#tags' do
user = create :user
get :tags, user_id: user.id
end
If the route is not to be unique per user the other option is a collection route, something like:
resources :users do
collection do
# creates /users/tags
get :tags
end
end

Resources