Cannot verify using rspec POST controller create action (devise and cancan)

I find it hard to get the rspec test to pass the controller. I would like to check if the POST action works. I use rails (3.0.3), cancan (1.4.1), devise (1.1.5), rspec (2.3.0)

The model is dead simple

class Account < ActiveRecord::Base attr_accessible :name end 

The controller is also standard (straight from the woods)

 class AccountsController < ApplicationController before_filter :authenticate_user!, :except => [:show, :index] load_and_authorize_resource ... def create @account = Account.new(params[:account]) respond_to do |format| if @account.save format.html { redirect_to(@account, :notice => 'Account was successfully created.') } format.xml { render :xml => @account, :status => :created, :location => @account } else format.html { render :action => "new" } format.xml { render :xml => @account.errors, :status => :unprocessable_entity } end end end 

and the rspec test I would like to pass, (sorry the name may not be the most suitable)

  it "should call create on account when POST create is called" do @user = Factory.create(:user) @user.admin = true @user.save sign_in @user #this is an admin post :create, :account => {"name" => "Jimmy Johnes"} response.should be_success sign_out @user end 

But all I get is

 AccountsController get index should call create on account when POST create is called Failure/Error: response.should be_success expected success? to return true, got false # ./spec/controllers/accounts_controller_spec.rb:46 

Other actions can be tested and passed (i.e. GET new)

here is a test for get new

 it "should allow logged in admin to call new on account controller" do @user = Factory.create(:user) @user.admin=true @user.save sign_in @user #this is an admin get :new response.should be_success sign_out @user end 

and to complete here is a feature file

 class Ability include CanCan::Ability def initialize(user) user ||= User.new if user.admin? can :manage, :all else can :read, :all end end end 

Any ideas? I assume that I am using rspec incorrect wait as the code really works (it is just that the test does not work as desired!)

+6
ruby-on-rails rspec devise cancan
source share
2 answers

response.should be_success returns true if the response code is in the range 200-299. But the create action is redirected, so the response code is set to 302, thus, a failure.

You can verify this using response.should redirect_to . Check the output of the standard RSpec controller generator for an example that might look like this:

  it "redirects to the created account" do Account.stub(:new) { mock_account(:save => true) } post :create, :account => {} response.should redirect_to(account_url(mock_account)) end 
+23
source share

The rspec test that passed the test was (thanks to the zetetic advice):

  it "should call create on account when POST create is called" do @user = Factory.create(:user) @user.admin = true @user.save sign_in @user #this is an admin account = mock_model(Account, :attributes= => true, :save => true) Account.stub(:new) { account } post :create, :account => {} response.should redirect_to(account_path(account)) sign_out @user end 
+3
source share

All Articles