rspec test of index, show all not archived - ruby-on-rails-3

Im trying to set up a (basic) test for a new feature I am going to implement. I have a job controller and instead of default showing all jobs I like to hide all the ones which is archived. I tried different ways but it seems like i am missing a piece or two of this puzzle. First i tried with calling 'visit' but get the message it does not exist. Second approach is using 'render' but that also ends up in a error saying render does not exists. (can i even use these methods in a controller spec?)
Is it wrong to put this in a controller test?
2 last test are causing errors
require "rails_helper"
require "spec_helper"
describe JobsController, :type => :controller do
context 'GET index' do
it "should be successful" do
get :index
expect(response).to be_success
end
it "renders the index template" do
get :index
expect(response).to render_template("index")
end
it "should not show any archived jobs as default" do
visit jobs_path
page.should have_no_content("Archived")
end
it 'should show the headers' do
render :template => 'job/index', :layout => 'layouts/job'
rendered.should_not contain('Archived')
end
end
end

Capybara is used for feature specs, and its matchers can be used in view specs.
Controller specs, by default, don't actually render the view because they're the wrong place to be checking for page content - https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs
You should probably move some of your tests to feature tests/view tests

Related

Rspec test :create method for a nested resource

I'm trying to set up specs to properly run with my nested resource.
This is the test code I'm trying to properly set up
it "redirects to the created unit" do
post :create, {:course_id => #course.id , :unit => valid_attributes}
response.should redirect_to(course_unit_path(#course, Unit.last))
end
That essentially should try to create a nested resource "unit" for "course".
Unfortunatly I'm getting the following error on all POST DELETE and PUT tests
Failure/Error: post :create, {:course_id => #course.id , :unit => valid_attributes}
NoMethodError:
undefined method `unit_url' for #<UnitsController:0x000000059f1000>
That makes sense since unit_url should be course_unit_url but it's RSpec calling it...
How can I make RSpec select the right named path?
For all GET tests I passed the :course_id by hand.
This is what I did:
it "redirects to the created unit" do
unit_id = "barry"
Unit.any_instance.should_receive(:save).and_return(true)
Unit.any_instance.stub(:id).and_return(unit_id)
post :create, {:course_id => #course.to_param , :unit => valid_attributes}
response.should redirect_to(course_unit_path(#course, unit_id))
end
I decided that the point of this test was not that it created a new model and redirected it, but simply that it redirects. I have another spec to ensure it creates a new model. Another benefit to this approach is that it doesn't touch the database so it should run a little faster.
I hope that helps.
Edit:
I also just noticed I have this in my before :each section which may be relevant:
Course.stub!(:find).and_return(#course)
Edit again:
In this case, there was code in the controller which was doing the offending call. As per comment below.

How do I test a controller with rspec-rails?

I want to test that my edit recipe page renders using rspec, though it doesn’t route to
recipes/edit
it routes to recipes/id/edit (id being replaced with a number)
my current test looks like this
describe "Show Edit Recipe Page" do
it "should display edit recipe page" do
get :edit
response.should be_success
response.should render_template(:edit)
end
end
how can i test this page correctly, at the moment my tests are failing
Problem
Your example doesn't include the code needed to actually test a controller object. RecipeController is not defined in your spec.
Solution
Make sure your controller specs live under spec/controllers or have an explicit type: :controller set. Then, actually describe a controller, either using the implicit subject or by setting up a controller instance in a before or test block. As the most basic example:
describe RecipeController do
# test something using the implied RecipeController.new
end
More Reading
RSpec Controller Specs
The get needs the id of the recipe passed in the params hash:
let(:recipe) { Factory.create(:recipe) }
it "should display edit recipe page" do
get :edit, :id => recipe.id
response.should be_success
response.should render_template(:edit)
end

RSpec Controller Testing - "assigns" behaving unexpectedly

I have built out a controller spec for my user model, and it passes, although I feel like it should fail. The spec is below:
describe UsersController do
include Devise::TestHelpers
let (:user) do
FactoryGirl.create(:user)
end
let (:other_user) do
FactoryGirl.create(:user)
end
before :each do
#request.env["devise.mapping"] = Devise.mappings[:user]
sign_in user
end
describe "#index" do
it "can GET 'index'" do
get :index
response.should be_success
end
it "populates a list of users" do
get :index
expect(assigns(:users)).to eq([user])
end
end
end
The line "expect(assigns(:users)).to eq([user])" passes, but I feel like it should fail. After all, I've created two users (user and other_user) up at the top. I'm clearly misunderstanding what's going on, so can someone explain it?
let is lazy; it won't create the user until the first time it's called (at which point the result is memoized). Since you call user but not other_user, only the first user is actually created, and the spec passes.
Use let! if you want eager evaluation, or make sure you call other_user somewhere.
RSpec Documentation

Rspec controller spec

I am new to Rspec please tell me what would be the controller Spec for the following two methods In index method only login page is seen by entering the username control goes to login method and find the name of person. If person is find then control goes to people path otherwise it goes back to root path that is index page it self.
class HomeController < ApplicationController
def index
end
def login
#person = Person.find(:all, :conditions => ['people.name =?', params[:person][:name]] )
if #person.blank?
redirect_to root_path
else
redirect_to people_path
end
end
end
Please help me.
Thanks.
Your rspec controller tests could be like this:
describe HomeController do
render_views
it "Logs in Person with non-blank name" do
person = Factory(:Person, name: "non-blank name")
get :login
response.should redirect_to(people_path)
end
it "does not log in Person with blank name" do
person = Factory(:Person, name: "") # blank name
get :login
response.should redirect_to(root_path)
end
end
Refer to rails controller specs for details.
EDIT:
Factory: the code that creates objects (test objects in this case). This is a preferred method for creating test objects because you can customize your code to create objects with varying attributes with least duplication.
Fixtures: If you are not using factories, you can specify the attributes for each of the objects you are going to create. For more than 2-3 object, this data quickly becomes unmanageable to maintain (for example, when you add an attribute, you need to make changes for each of these objects).
Stubs: If you prefer not to create database records while creating model objects, you can stub the model code white testing controllers.
For more information, refer:
1. testing guide
2. asciicast (Note: this code refers to an older version of FactoryGirl gem. Refer below for up-to-date API of FactoryGirl)
3. FactoryGirl Readme

Rails 3 + Rspec 2: Testing content_for

I'm running Rails 3.1.1, RSpec 2.7.0 and HAML 3.1.3.
Say I have the following view files:
app/views/layouts/application.html.haml
!!!
%html
%head
%title Test
= stylesheet_link_tag "application"
= javascript_include_tag "application"
= csrf_meta_tags
%body
= content_for?(:content) ? yield(:content) : yield
app/views/layouts/companies.html.haml
- content_for :content do
#main
= yield :main
#sidebar
= yield :sidebar
= render :template => 'layouts/application'
app/views/companies/index.html.haml
- content_for :main do
%h1 MainHeader
- content_for :sidebar do
%h1 SidebarHeader
And the following spec file:
spec/views/companies/index_spec.rb
require 'spec_helper'
describe 'companies/index.html.haml' do
it 'should show the headers' do
render
rendered.should contain('MainHeader')
rendered.should contain('SidebarHeader')
end
end
When I run RSpec, I get the following error:
1) companies/index.html.haml should show the headers
Failure/Error: rendered.should contain('MainHeader')
expected the following element's content to include "MainHeader":
# ./spec/views/companies/index_spec.rb:7:in `block (2 levels) in <top (required)>'
At first, I thought RSpec was somehow missing the content_for blocks when rendering the view files. However, I was not able to find any issue related to it on RSpec's github repository, so I'm not sure who's to blame here.
One (recent) solution I found is at http://www.dixis.com/?p=571. However, when I try the suggested code
view.instance_variable_get(:#_content_for)
it returns nil.
Is there a way to test content_for in view specs?
Is there a better way to structure my layout files, such that I'm actually able to test them and still achieve the same end result?
Using Rspec 2 with Rails 3, in order to write view specs for usage of content_for, do this:
view.content_for(:main).should contain('MainHeader')
# instead of contain() I'd recommend using have_tag (webrat)
# or have_selector (capybara)
p.s. the value of a content_for(...) block by default is an empty string, so if you want to
write specs showing cases in which content_for(:main) does not get called, use:
view.content_for(:main).should be_blank
Your spec could be written as:
it "should show the headers" do
render
view.content_for(:main).should contain('MainHeader')
view.content_for(:side_header).should contain('SidebarHeader')
end
This way your spec shows exactly what your view does, independent of any layout. For a view spec, I think it's appropriate to test it in isolation. Is it always useful to write view specs? That's an open question.
Instead if you want to write specs showing what the markup served to the user looks like, then you'll want either a request spec or a cucumber feature. A third option would be a controller spec that includes views.
p.s. if you needed to spec a view that outputs some markup directly and delegates other markup to content_for(), you could do that this way:
it "should output 'foo' directly, not as a content_for(:other) block" do
render
rendered.should contain('foo')
view.content_for(:other).should_not contain('foo')
end
it "should pass 'bar' to content_for(:other), and not output 'bar' directly" do
render
rendered.should_not contain('bar')
view.content_for(:other).should contain('bar')
end
That would probably be redundant, but I just wanted to show how render() populates rendered and view.content_for. "rendered" contains whatever output the view produces directly. "view.content_for()" looks up whatever content the view delegated via content_for().
From the RSpec docs:
To provide a layout for the render, you'll need to specify both the template and the layout explicitly.
I updated the spec and it passed:
require 'spec_helper'
describe 'companies/index.html.haml' do
it 'should show the headers' do
render :template => 'companies/index', :layout => 'layouts/companies'
rendered.should contain('MainHeader')
rendered.should contain('SidebarHeader')
end
end
Do not bother with view specs. They're hard to write well, and they don't test enough of the stack to be worth using (especially in view of the difficulty writing). Instead, use Cucumber, and test your views in the course of that.
You generally don't want to test content_for specifically either: that's implementation, and you should instead be testing behavior. So just write your Cucumber stories so they test for the desired content.
If for some odd reason you do need to test content_for, RSpec has a syntax that's something like body[:content_name] or body.capture :content_name depending on the version (or something like that; haven't used it in a while). But consider carefully whether there's a better way to test what you actually want to test.