Response. Should be_success returns true if the response code is in the range 200-299. But the create action redirects, so the response code gets set to 302, thus the failure.
Up vote 2 down vote favorite share g+ share fb share tw.
I am having difficulty getting a rspec test for a controller to pass. I would like to test that the POST create action works. I am using rails (3.0.3), cancan (1.4.1), devise (1.1.5), rspec (2.3.0) The model is dead simple class Account :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 is (excuse the title, perhaps not the most appropriate one) 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 Yet 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 do pass (i.e. GET new) here is the 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 for completion here is the ability 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?
My guess is that I am using the wrong rspec expectation, since the code does work (it is just that the test does not perform as desired! ) ruby-on-rails rspec devise cancan link|improve this question edited Jan 3 '11 at 21:04zetetic14.2k11428 asked Jan 3 '11 at 9:00Dimitris454212 56% accept rate.
Response. Should be_success returns true if the response code is in the range 200-299. But the create action redirects, so the response code gets set to 302, thus the failure.
You can test this by using response. Should redirect_to. Check the output of the standard RSpec controller generator for an example, which 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.
Zetetic, your answer was excellent. I had to make a few tweeks to get it to pass, but the gist of it was that it should have been a redirect. – Dimitris Jan 4 '11 at 10:42.
The rspec test that got the test to pass was (thanks to zetetic's 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.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.