Joseph N.
Joseph N.

Reputation: 2457

Test failing with RSpec in my rails app

I am trying to run this test but for some reason its failing.

describe "User Pages" do
    subject { page }
    .
    .
describe "Sign up" do
    before { visit signup_path }

    let(:submit) { "Sign Me Up" }
    .
    .
 describe "with valid information" do
  before do
    fill_in "name",             with: "newuser"
    fill_in "Username",         with: "user254"
    fill_in "Email",            with: "[email protected]"
    select  "Male",             from: "gender"        
    fill_in "Password",         with: "password"
    fill_in "Confirm Password", with: "password"
  end

  it "Should create a user" do
    expect { click_button submit}.to change(User, :count).by(1)
  end   
end

However, when I run the test I get this error

Failures:

  1) User Pages signup with valid information Should create a user
      Failure/Error: expect { click_button submit}.to change(User, :count).by(1)
       count should have been changed by 1, but was changed by 0
      # ./spec/requests/user_pages_spec.rb:35:in `block (4 levels) in <top (required)>'

      Finished in 4.61 seconds
      35 examples, 1 failure

      Failed examples:

      rspec ./spec/requests/user_pages_spec.rb:34 # User Pages signup with valid information Should create a user

This is my create action in my controller

def create
   @user = User.new(params[:user])
   if @user.save
     redirect_to @user
   else
     render 'new'
   end
end

When I try signing up in the browser, signup is successfull. Is anything wrong with my test? Thanks

Upvotes: 0

Views: 422

Answers (2)

arieljuod
arieljuod

Reputation: 15838

check if the user is being saved on the controller:

def create
   @user = User.new(params[:user])
   puts "valid? = #{@user.valid?.inspect}"
   if @user.save
     puts 'saved!'
     redirect_to @user
   else
     puts 'not saved'
     puts @user.errors.inspect
     render 'new'
    end
end

that will print the controller process and you will know what's happening

if nothing gets printed, then the problem is some before_filter that's preventing the create action to being called

Upvotes: 1

BvuRVKyUVlViVIc7
BvuRVKyUVlViVIc7

Reputation: 11821

try it with

it "Should create a user" do
    click_button submit
    User.count.should == 1
  end  

Upvotes: 0

Related Questions