Reputation: 447
I have browsed google and stackoverflow for my question and have found some similar ones, but none that solve my problem.
In my app a User has_one Profile and a Profile belongs_to User.
I am trying to test some User features, and I need to create a test Profile associated with my test User to properly do so.
Here is my factories/user_factory.rb
FactoryGirl.define do
factory :user do
email {Faker::Internet.safe_email}
password "password"
password_confirmation "password"
end
end
Here is my factories/profile_factory.rb
FactoryGirl.define do
factory :profile do
phone Faker::PhoneNumber.phone_number
college Faker::University.name
hometown Faker::Address.city
current_location Faker::Address.city
about "This is an about me"
words_to_live_by "These are words to live by"
first_name {Faker::Name.name}
last_name {Faker::Name.name}
gender ["male", "female"].sample
user
end
end
Here is my features/users_spec.rb where I need to create my associated profile:
require 'rails_helper'
feature "User accounts" do
before do
visit root_path
end
let(:user) {create(:user)}
let(:profile) {create(:profile, user: user)}
scenario "create a new user" do
fill_in "firstName", with: "First"
fill_in "lastName", with: "Last"
fill_in "signup-email", with: "[email protected]"
fill_in "signup-password", with: "superpassword"
fill_in "signup-password-confirm", with: "superpassword"
#skip birthday=>fill_in "birthday", with:
#skip gender
expect{ click_button "Sign Up!"}.to change(User, :count).by(1)
end
scenario "sign in an existing user" do
sign_in(user)
expect(page).to have_content "Signed in successfully"
end
scenario "a user that is not signed in can not view anything besides the homepage" do
end
end #user accounts
The scenario sign in an existing user is where I need my associated profile.
Right now I am just creating a profile using the factory
let(:profile) {create(:profile, user: user)}
I have tried passing the create block to associate the profile, and I've tried overriding the user_id attribute of the profile to associate it with the created user, but neither have worked. Ideally I would like to set it up so that whenever a user is created an associated profile is created for it. Any ideas?
I know this can't be too hard I just haven't been able to come up with a solution. Thanks for the help.
Upvotes: 0
Views: 1716
Reputation: 8602
The simplest way is to have a factory with the same name as the association. In your case, if the association is profile, and you can implicitly create the associated profile record along with the user record. Just use the name of the associated factory, like this.
factory :user do
...
profile
end
If you need more control, Factory Girl's association is what you need. You can override attributes and select a factory name different form the association name. Here, the association name is prof and the factory is profile. The lastName field is overridden.
factory :user do
...
association :prof, factory: :profile, lastName: "Johnson"
end
You can find more info at Factory Girl's Getting Started.
Upvotes: 1