0
votes

I am trying to test my signup and sign in through devise using rspec, capybara and factoryGirl

the problem is that the tests keep failing in the signin test with this message from activeRecord:

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

these are my tests:

describe "GET /users/sign_up" do
it "register user and send email" do

  user = FactoryGirl.build(:user)
  visit new_user_registration_path
  fill_in "Email", :with => user.email
  fill_in "Password", :with => user.password
  fill_in "Password confirmation", :with => user.password
  fill_in "Name", :with => user.name
  click_button "Sign up"
  ActionMailer::Base.deliveries.last.to.should include(user.email)
  page.should_not have_content("Password confirmation")
end
end

and the signin test:

describe "GET /users/sign_in" do
it "try to login email password" do
  user = FactoryGirl.create(:user)
  visit new_user_session_path
  fill_in "Email", :with => user.email
  fill_in "Password", :with => user.password
  click_button "Sign in"
  page.should have_content("Signed in successfully.")
end
end

while searching for a solution I saw some people added database_cleaner and said that suppose to help. so i added the database_cleaner gem and added these lines to my spec_helper.rb file:

config.use_transactional_fixtures = false

config.before(:suite) do
  DatabaseCleaner.strategy = :truncation
end

config.before(:each) do
  DatabaseCleaner.start
end

config.after(:each) do
  DatabaseCleaner.clean
end

but it still not working.

would love some guidance or idea. thanks!

adding factory girl code:

require 'factory_girl'

FactoryGirl.define do

factory :user do |f|
    f.sequence(:email) {|n| "dodo#{n}@gmail.com"}
    f.password "secret123"
    f.name "dodo"
end

factory :partner do |f|
    f.company "spectoos"
end

end
1
can you include the factory code for user?toms
just a sanity check, but is it possible the user exist from previous tests? Have you tried dropping all users from the test db?toms
isn't the cleanup suppose to do that?... and I don't have other tests accept these two. I think it passes the first register test then fails the sign in testguy schaller

1 Answers

0
votes

I found my problem. the test was failing because the user that I created with factoryGirl is not confirmed. and I have the confirmable on for devise.

any after this line:

user = FactoryGirl.create(:user)

I added this line:

user.confirm!

and now its fine.

thanks