2
votes

I'm trying to run a Capybara 1.0 test on my Rails 3 app to test whether when a user clicks on a confirmation link, he is actually confirmed.

Now, this actually works when I test it manually. In addition, as you can see there is a puts @user.confirmed line that I put in the confirm method to debug this, and it actually prints true when I run the test. However, the test itself fails.

It seems as if the confirmed attribute in my user model isn't being remembered by the test after executing the controller method.

What am I missing? Thanks so much in advance.

Test:

it "should allow a user to be confirmed after clicking confirmation link" do
  fill_in('user_email', :with => '[email protected]')
    click_button('Submit')
    @user = User.find_by_email('[email protected]')
    @user.confirmed.should be_false
    visit confirm_path(@user.confirmation_code)
    @user.confirmed.should be_true
end

Controller method:

def confirm
    @confirmation_code = params[:confirmation_code]
    @user = User.find_by_confirmation_code(@confirmation_code)
    @website = @user.website
    @user.confirm
    if @user.referrer_id
        User.find(@user.referrer_id).increment_signups
    end
    flash[:success] = "Thanks for signing up!" 
    flash[:user_show] = @user.id
            puts @user.confirmed
    redirect_to "http://" + @website.domain_name
end

User model method:

def confirm
    self.confirmed = true
    self.save
end
1

1 Answers

4
votes

Is it that you would need to reload the user object after visiting the confirm_path? Try this:

it "should allow a user to be confirmed after clicking confirmation link" do
  fill_in('user_email', :with => '[email protected]')
    click_button('Submit')
    @user = User.find_by_email('[email protected]')
    @user.confirmed.should be_false
    visit confirm_path(@user.confirmation_code)
    @user = User.find_by_email('[email protected]')
    @user.confirmed.should be_true
end

Alternatively you could use @user.reload.

The user object referred to in your test is just a copy of the object being manipulated by the application so it is not going to be automatically refreshed. You need to fetch it from the database a second time to get the updated values.