Question

I am learning rais from the rails tutorial book: http://ruby.railstutorial.org/chapters/sign-in-sign-out#fnref:9.14

I am working on the exercise. The assignment is to create a sign in method in spec helper so that it can be used in integration tests. He gave the code already:

def integration_sign_in(user)
    visit signin_path
    fill_in :email,    :with => user.email
    fill_in :password, :with => user.password
    click_button
  end

So, in my layout_links_spec.rb integration test, I plan to use it.

before(:each) do
      @user = Factory(:user)
      visit signin_path
      fill_in :email,    :with => @user.email
      fill_in :password, :with => @user.password
      click_button
      # integration_sign_in(Factory(:user))
    end

I comment everything out and use integration_sign_in(Factory(:user)). The error I get is

ActiveRecord::RecordInvalid:
       Validation failed: Email has already been taken

However, if I use the original which looks so similar to the integration_sign_in function, the tests passes. Can someone provide an explanation please?

Thanks.

Was it helpful?

Solution

Sounds like your :user factory is returning the same email address each time it makes a user. Because you have the before(:each) it's going to be making and stuffing into the DB more than one user, and they will all have the same email.

If you post your factory we can probably tweak it to use a sequence or find an alternative solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top