I think this could be related to the issues I described in this question.
I can’t figure out why Capybara is not able to test the sign up form on my rails app when using Factory Girl to create user Factories. I keep getting a email has already been taken error. Here’s my Factory:
FactoryGirl.define do
sequence :email do |n|
"email#{n}@example.com"
end
factory :user do
email
password "secret"
password_confirmation "secret"
end
end
Here’s my sign up test:
require "test_helper"
describe "Signup integration" do
before(:each) do
visit signup_path
end
it "successfully routes to the signup page" do
page.text.must_include "Sign Up"
end
it "signs up a new user" do
user = FactoryGirl.create(:user)
fill_in "user_email", :with => user.email
fill_in "Password", :with => user.password
fill_in "Password confirmation", :with => user.password_confirmation
click_button "Create User"
current_path == "/"
page.text.must_include "Signed up!"
end
end
And here’s my test_helper.rb:
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "minitest/autorun"
require "capybara/rails"
require "active_support/testing/setup_and_teardown"
class IntegrationTest < MiniTest::Spec
include Rails.application.routes.url_helpers
include Capybara::DSL
register_spec_type(/integration$/, self)
def last_email
ActionMailer::Base.deliveries.last
end
def reset_email
ActionMailer::Base.deliveries = []
end
end
class HelperTest < MiniTest::Spec
include ActiveSupport::Testing::SetupAndTeardown
include ActionView::TestCase::Behavior
register_spec_type(/Helper$/, self)
end
Turn.config.format = :outline
I’m not really sure what could be wrong with that. If I add the Capybara save_and_open_page method to each line it is able to get all the way to the password field, but Capybara is not able to fill out the password field. It adds a unique email address like email3@example.com to the email field, but then can’t add the Factory Girl password. If I put a plain text password into the test (fill_in "Password", :with => "password") it is able to fill out the field, but that doesn’t seem like the proper way to test this.
I’m also not sure if it could be related to a login test that I have in the app as well? Could the issue be that Capybara is logged in as another user from the login test? If so, how do you clean out the session your tests?
Finally, here’s my gemfile, in case that is relevant:
source 'https://rubygems.org'
gem 'rails', '3.2.8'
gem 'jquery-rails'
gem 'pg'
gem 'heroku'
gem 'taps'
gem 'sorcery'
gem 'bootstrap-sass'
gem 'simple_form'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
group :test do
gem 'minitest'
gem 'capybara'
gem 'capybara_minitest_spec'
gem 'turn'
gem 'factory_girl_rails'
end
The problem is probably in this line
try to change it to
The
FactoryGirl.createcreates an instance of User object and saves it to the database.buildcreates an instance, but does not save it to the DB.