ruby-on-rails - Rspec工厂机器人postgres UUID问题
问题描述
我正在使用 rspec 功能规范中的工厂机器人创建用户对象。
用户属于一家公司并具有以下出厂设置
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
结尾
解决方案
检查开发和测试数据库的架构是否相同。删除和重新创建测试数据库的最简单方法
RAILS_ENV=test rake db:drop
RAILS_ENV=test rake db:setup
确保您有 UUID 类型 IDschema.rb
推荐阅读
- sql - 如果 CSV 没有所有表格字段,如何将 CSV 导入表格?
- javascript - React 中的 PrivateRouter 组件
- apache-kafka-streams - 如何为spring kafka流中的多个绑定设置UncaughtExceptionHandlers?
- python - 如何显式查找构建的 Tensorflow 模型并提取模型预测
- google-apps-script - 如何在 AppMaker 的 MultiSelect 小部件中手动设置值?
- python - 如何在我的项目中正确加载 Python 类?
- r - 如何使用 Slurm 在多个节点上发送循环?
- excel - 如何按单独的列有条件地排序?
- java - 在Java中以O(N)而不是O(NlogN)构建堆
- sql - 嵌套事务找不到保存点