我创建工厂使用机器人在一个RSpec功能规范中的一个用户对象。
belongs_to的一个公司的用户,并具有以下工厂设置
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
当我创建通过轨道用户控制台工厂将按预期
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: "[email protected]"...
但是当我使用rspec的功能规格内同一个工厂都ID和的company_id字段保存为整数不是的UUID
#<User id: 288, company_id: 0, first_name: "Bob", last_name: "Deckow", email: "[email protected]"...
在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
结束
请检查您是否为开发和测试数据库相同的模式。只是删除并重新测试DB最简单的方法
RAILS_ENV=test rake db:drop
RAILS_ENV=test rake db:setup
请确保您有schema.rb
UUID类型ID