1
votes

I want to thank you for your help in advance! I have just started to learn testing with RSpec, FactoryGirl & Capybara. My feature tests are failing for what seems to be a duplicate user name. I already have db cleaner gem installed. Also, when I check the test db through console, there are no user records. Can you please help me understand what I am doing wrong?

Below are my two feature tests and errors.

1

require "rails_helper"
feature "Creating Articles" do
let(:user) {FactoryGirl.create(:user)}
let(:article) {FactoryGirl.create(:article)}

    def fill_in_signin_fields
        click_link("Sign in")
        fill_in "user[email]",        with: user.email
        fill_in "user[password]",     with: user.password
        click_button "Log in"
    end

    scenario "A user creates a new article" do
        visit '/'

        click_link("New Article")
        expect(page).to have_text "You need to sign in or sign up before continuing."

        fill_in_signin_fields
        click_link("New Article")

        fill_in "Title",    with: article.title
        fill_in "Body",     with: article.body
        click_button "Create Article"

        #expect(page).to have_content article.title
        #expect(page).to have_content article.body


    end
end

Creating Articles A user creates a new article Failure/Error: let(:article) {FactoryGirl.create(:article)}
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: users.name: INSERT INTO "users" ("name", "created_at", "updated_at", "email", "encrypted_password") VALUES (?, ?, ?, ?, ?)

2

require 'rails_helper'

feature "sign up" do
let(:user) {FactoryGirl.create(:user)}

    def fill_in_singup_fields
        fill_in "user[name]",                  with: user.name
        fill_in "user[email]",                 with: user.email
        fill_in "user[password]",              with: user.password
        fill_in "user[password_confirmation]", with: user.password
        click_button "Sign up"
    end

    scenario 'a user signs up' do
        visit root_path
        click_link "Sign up"
        fill_in_singup_fields
        expect(page).to have_content("Welcome! You have signed up successfully.")
    end 

end

sign up a user signs up Failure/Error: expect(page).to have_content("Welcome! You have signed up successfully.") expected to find text "Welcome! You have signed up successfully." in "Blogger New Article Submit Link Sign up Sign in Sign up × Email has already been taken Username Email Password (6 characters minimum) Password confirmation Log in"

1

1 Answers

0
votes

When you create a factory, it is inserted into the test database. You are getting these errors because the article and user you wish to create are already created.

When testing create functionality (in this case, for articles and users), you don't want to create factory objects as well.

Solution: remove factories and fill_in with hard-coded strings.

E.g.,

fill_in "user[name]", with: "TestUser"