RSpec 功能测试 Active Record 非唯一错误
RSpec Feature Test Active Record Not-Unique Error
在此先感谢您的帮助!我刚刚开始学习使用 RSpec、FactoryGirl 和 Capybara 进行测试。我的功能测试因似乎是重复的用户名而失败。我已经安装了 db cleaner gem。另外,当我通过控制台检查 test db 时,没有用户记录。
你能帮我理解我做错了什么吗?
以下是我的两个功能测试和错误。
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"
创建工厂时,它被插入到测试数据库中。您收到这些错误是因为您要创建的文章和用户已经创建。
测试创建功能时(在本例中,针对文章和用户),您不希望也创建工厂对象。
解决方案:删除带有硬编码字符串的工厂和 fill_in
。
例如,
fill_in "user[name]", with: "TestUser"
在此先感谢您的帮助!我刚刚开始学习使用 RSpec、FactoryGirl 和 Capybara 进行测试。我的功能测试因似乎是重复的用户名而失败。我已经安装了 db cleaner gem。另外,当我通过控制台检查 test db 时,没有用户记录。 你能帮我理解我做错了什么吗?
以下是我的两个功能测试和错误。
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"
创建工厂时,它被插入到测试数据库中。您收到这些错误是因为您要创建的文章和用户已经创建。
测试创建功能时(在本例中,针对文章和用户),您不希望也创建工厂对象。
解决方案:删除带有硬编码字符串的工厂和 fill_in
。
例如,
fill_in "user[name]", with: "TestUser"