1

I'm creating a user object using factory bot in an rspec feature spec.

The user belongs_to a company and has the below factory setup

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

When I create a user via rails console the factory works as expected

user = FactoryBot.create(:user)

The company is created and I can see the company_id as a UUID on the object

#<User id: "bb9fd4c7-bdce-4338-a42d-723876f514bc", company_id: "41e35b15-d766-4b1a-b833-bf1df4241064", first_name: "Frank", last_name: "Grimes", email: "[email protected]"...

But when I use the same factory inside rspec feature spec both id and company_id fields are saved as integers not UUIDs

#<User id: 288, company_id: 0, first_name: "Bob", last_name: "Deckow", email: "[email protected]"...

The user creation in rspec is below

let(:user) { create(:user) }

Is there any reason why rspec would have this effect? Full feature spec below:

# 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

end

1 Answer 1

1

Check that you have the same schema for development and test databases. Easiest way to just drop and recreate testing DB

RAILS_ENV=test rake db:drop
RAILS_ENV=test rake db:setup

Ensure that you have UUID type ids in schema.rb

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, thank you, thank you. Was banging my head against the wall on that one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.