0

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

1
  • Have you tried outputting any errors on the @user object? That might provide you with some extra insight. Commented Jan 20, 2013 at 19:36

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

This is what gets printed #<ActiveModel::Errors:0x007f8ff059f718 @base=#<User id: nil, name: "", email: "", created_at: nil, updated_at: nil, password_digest: nil, username: "", gender: "", remember_token: nil>, @messages={:password_digest=>["can't be blank"], :name=>["can't be blank"], :email=>["can't be blank", "is invalid"], :username=>["can't be blank"], :gender=>["can't be blank"],:password=>["is too short (minimum is 6 characters)"], :password_confirmation=>["can't be blank"]}>
check that you are filling the right names of the form, I suppose the form fileds are something like "user[name]" or "user_name", not just "name" if you created them with the form_for helper, that's why the record has everything blank, params[:user] is empty, you are not filling the form the right way, inspect the form with a browser to see the form field's names/ids
Thanks alot! Worked like a charm.
0

try it with

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

2 Comments

I still get an error Failure/Error: User.count.should == 1 expected: 1 got: 0 (using ==)
then try to use @user.save! in your controller, then you should get an error why its not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.