我正在使用rspec功能规范中的factory bot创建用户对象。
用户属于某公司,并且具有以下出厂设置
FactoryBot.define do
factory :user do
company
auth_token { Faker::Internet.password }
email { Faker::Internet.email }
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
contact_phone { '+353871234567' }
status { true }
password { Faker::Internet.password }
password_confirmation { password }
end
end
当我通过Rails控制台创建用户时,工厂将按预期工作
user = FactoryBot.create(:user)
公司已创建,我可以在对象上看到company_id作为UUID
#<User id: "bb9fd4c7-bdce-4338-a42d-723876f514bc", company_id: "41e35b15-d766-4b1a-b833-bf1df4241064", first_name: "Frank", last_name: "Grimes", email: "drewshields@gislason.co"...
但是当我在rspec功能规范中使用相同的工厂时,id和company_id字段都保存为整数而不是UUID
#<User id: 288, company_id: 0, first_name: "Bob", last_name: "Deckow", email: "raleighharber@leannon.info"...
rspec中的用户创建如下
let(:user) { create(:user) }
rspec会产生这种效果吗? 完整功能说明如下:
# frozen_string_literal: true
# Specs for password reset
require 'rails_helper'
describe 'password reset', type: :feature do
let(:user) { create(:user) }
let(:invaliduser) {
invalid = build(:user, first_name: nil, contact_phone: '123')
invalid.save(validate: false)
invalid
}
context 'active user resets password' do
it 'sends an email to the user' do
visit root_path
first(:link, 'Login').click
click_on 'Forgotten Password?'
fill_in 'Email', with: user.email
click_on 'Reset Password'
expect(page).to have_content('If a matching email was found an email has been sent with password reset instructions.')
expect(ActionMailer::Base.deliveries.last.to).to include(user.email)
expect(ActionMailer::Base.deliveries.last.subject).to include('Please reset your password')
end
end
context 'inactive user resets password' do
it 'sends an email to the {{__}} team' do
visit root_path
first(:link, 'Login').click
click_on 'Forgotten Password?'
fill_in 'Email', with: invaliduser.email
click_on 'Reset Password'
expect(page).to have_content('If a matching email was found an email has been sent with password reset instructions.')
expect(ActionMailer::Base.deliveries.last.to).to include('{{team_email}}')
expect(ActionMailer::Base.deliveries.last.subject).to include('User Account Recovery')
end
end
结束
答案 0 :(得分:1)
检查用于开发和测试数据库的架构是否相同。 删除并重新创建测试数据库的最简单方法
RAILS_ENV=test rake db:drop
RAILS_ENV=test rake db:setup
确保您在schema.rb
中有UUID类型ID