RSpec功能测试活动记录不唯一错误

时间:2017-03-01 16:02:54

标签: rspec factory-bot

我想提前感谢你的帮助!我刚开始用RSpec,FactoryGirl& amp;水豚。我的功能测试失败了似乎是一个重复的用户名。我已经安装了 db cleaner gem 。此外,当我通过控制台检查测试数据库时,没有用户记录。 你能帮我理解我做错了吗?

以下是我的两个功能测试和错误。

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
  

创建文章用户创建新文章        失败/错误:让(:article){FactoryGirl.create(:article)}
  ActiveRecord的:: RecordNotUnique:          SQLite3 :: ConstraintException:UNIQUE约束失败:users.name:INSERT INTO“users”(“name”,“created_at”,“updated_at”,“email”,“encrypted_pa​​ssword”)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
  

注册用户注册        失败/错误:期待(页面).to have_content(“欢迎!您已成功注册。”)          期待找到文字“欢迎!您已成功注册。”在“Blogger新文章提交链接注册登录注册×已发送电子邮件用户名电子邮件密码(最少6个字符)密码确认登录”

1 个答案:

答案 0 :(得分:0)

创建工厂时,会将其插入测试数据库。您将收到这些错误,因为您已创建了要创建的文章和用户。

在测试创建功能时(在本例中,对于文章和用户),您也不想创建工厂对象。

解决方案:使用硬编码字符串删除工厂和fill_in

如,

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