RSpec功能测试活动记录非唯一错误
我想提前感谢你的帮助!我刚刚开始学习使用RSpec、FactoryGirl和水豚进行测试。我的功能测试失败,因为它似乎是一个重复的用户名。我已经安装了db cleaner gem。另外,当我通过控制台检查测试数据库时,没有用户记录。 你能帮我理解我做错了什么吗 下面是我的两个功能测试和错误 1. 创建文章用户创建新文章 失败/错误:let(:article){FactoryGirl.create(:article)}RSpec功能测试活动记录非唯一错误,rspec,factory-bot,Rspec,Factory Bot,我想提前感谢你的帮助!我刚刚开始学习使用RSpec、FactoryGirl和水豚进行测试。我的功能测试失败,因为它似乎是一个重复的用户名。我已经安装了db cleaner gem。另外,当我通过控制台检查测试数据库时,没有用户记录。 你能帮我理解我做错了什么吗 下面是我的两个功能测试和错误 1. 创建文章用户创建新文章 失败/错误:let(:article){FactoryGirl.create(:article)} ActiveRecord::RecordNotUnique: SQLite3:
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException:唯一约束失败:users.name:插入“users”(“name”、“created_at”、“updated_at”、“email”、“encrypted_password”)值(?,,,,?) 2. 注册一个用户注册了 失败/错误:期望(第页)。包含内容(“欢迎!您已成功注册。”) 预计会找到文本“欢迎!您已成功注册。”在“Blogger新文章提交链接注册注册注册注册×电子邮件已使用用户名电子邮件密码(至少6个字符)密码确认登录”
创建工厂时,它将插入到测试数据库中。您收到这些错误是因为您要创建的文章和用户已经创建 在测试创建功能时(在本例中,针对文章和用户),您也不希望创建factory对象 解决方案:删除工厂并用硬编码字符串填写 例如:
在“user[name]”中填写:“TestUser”
当您创建工厂时,它会插入到测试数据库中。您收到这些错误是因为您要创建的文章和用户已经创建
在测试创建功能时(在本例中,针对文章和用户),您也不希望创建factory对象
解决方案:删除工厂并用硬编码字符串填写
例如:
在“user[name]”中填写:“TestUser”
迈克,谢谢你的帮助!它解决了唯一性问题。迈克,谢谢你的帮助!它解决了唯一性问题。
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
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