shoen
shoen

Reputation: 11865

Rspec tests fail in similar situations

I have the following methods in my controller:

def create
  @user = User.new(params[:user])
    if @user.save        
      redirect_to log_in_path, notice: 'Signed up!'
    else
      render "new"
    end
end

def destroy
  @user = User.find(params[:id])
  @user.destroy
    redirect_to users_path, notice: 'User was deleted.'
end

and specs:

describe "POST 'create'" do
  it "should allow create user" do
    @user = Factory(:user)
    post 'create', :id => @user
    response.should be_success
  end

describe "DELETE 'destroy'" do     
  it "should delete the user" do
    @user = Factory(:user)
    delete "destroy", :id => @user
    response.should redirect_to users_path
  end
end

I don't understand why the test for create method is passing even if there is a redirect right after the object is saved. if i set:

  response.should redirect_to log_in_path

as expectation the test will fail, same for destroy method, if I switch expectation to:

response.should be_success

it will fail. Any suggestions? Thanks

Upvotes: 0

Views: 178

Answers (1)

Mirko
Mirko

Reputation: 5286

The actual response of your create action is 200 OK (even with your model being invalid and not saved) and that's what you are testing right now.

Other than that, your create test is no good. Create action takes user hash as param, not the user's id. You probably want something like this:

  it "should allow create user" do
    @user = Factory.attributes_for(:user)
    post 'create', :user => @user
    @user.persisted?.should be_true
    response.should be_redirect
  end

Upvotes: 3

Related Questions