请注意,我在创建用户时出现此错误,但在编辑时出现。
我相信电子邮件唯一性检查可以在devise
内完成。
Github repo:https://github.com/tenzan/shop,但editing_users_spec.rb
尚不存在。
$ bundle exec rspec spec/features/admin/editing_users_spec.rb
FF
Failures:
1) Admins can change a user's details with valid details
Failure/Error: let(:user) {FactoryGirl.create(:user)}
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
# ./spec/features/admin/editing_users_spec.rb:5:in `block (2 levels) in <top (required)>'
# ./spec/features/admin/editing_users_spec.rb:9:in `block (2 levels) in <top (required)>'
2) Admins can change a user's details when toggling a user's admin ability
Failure/Error: let(:user) {FactoryGirl.create(:user)}
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
# ./spec/features/admin/editing_users_spec.rb:5:in `block (2 levels) in <top (required)>'
# ./spec/features/admin/editing_users_spec.rb:9:in `block (2 levels) in <top (required)>'
Finished in 0.05535 seconds (files took 2.29 seconds to load)
2 examples, 2 failures
Failed examples:
rspec ./spec/features/admin/editing_users_spec.rb:13 # Admins can change a user's details with valid details
rspec ./spec/features/admin/editing_users_spec.rb:22 # Admins can change a user's details when toggling a user's admin ability
规格/特征/管理/ editing_users_spec.rb :
require 'rails_helper'
RSpec.feature "Admins can change a user's details" do
let(:admin_user) { FactoryGirl.create(:user, :admin)}
let(:user) {FactoryGirl.create(:user)}
before do
login_as(admin_user)
visit admin_user_path(user)
click_link 'Edit User'
end
scenario 'with valid details' do
fill_in 'Email', with: 'newuser@example.com'
click_button 'Update User'
expect(page).to have_content 'User has been updated.'
expect(page).to have_content 'newuser@example.com'
expect(page).not_to have_content user.email
end
scenario "when toggling a user's admin ability" do
check 'Is an admin?'
click_button 'Update User'
expect(page).to have_content 'User has been updated.'
expect(page).to have_content "#{user.email} (Admin)"
end
end
答案 0 :(得分:2)
在您的工厂为您提供......
sequence(:email) { |n| 'test#{n}@example.com' }
应该是......
sequence(:email) { |n| "test#{n}@example.com" }
您需要双引号来进行n