私は私の質問のためにgoogleとstackoverflowをブラウズして、いくつかの似たようなものを見つけましたが、私の問題を解決するものはありません。FactoryGirlとRspecとのhas_one関連の子オブジェクトを作成するにはどうすればよいですか?
私のアプリでは、ユーザーhas_oneプロファイルとプロファイルbelongs_toユーザー。
私はいくつかのユーザー機能をテストしようとしており、適切に行うにはテストユーザーに関連するテストプロファイルを作成する必要があります。ここで
は私の工場/ user_factory.rbここFactoryGirl.define do
factory :user do
email {Faker::Internet.safe_email}
password "password"
password_confirmation "password"
end
end
である私の工場/ 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
である私の機能私は私の関連するプロファイルを作成する必要があります/ users_spec.rb :
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
既存のユーザーのシナリオ記号は、関連するプロファイルが必要な場所ですe。
今私はちょうど私は、プロファイルを関連付けるためにブロックを作成して渡して試してみました、と私はそれを関連付けるために、プロファイルのuser_idの属性をオーバーライドしようとした工場
let(:profile) {create(:profile, user: user)}
を使用してプロファイルを作成しています作成されたユーザーが、どちらも機能していません。理想的には、ユーザーを作成するたびに関連プロファイルが作成されるように設定したいと考えています。何か案は?
私はこれがあまりにも難しいことは知っている私は解決策を思い付くことができませんでした。助けてくれてありがとう。
これは私の問題を解決すると信じていますが、今は私のuser_factoryでこの行を指しているスタックレベルが非常に深刻なエラーになっています:email {Faker :: Internet.safe_email} – srlrs20020
ああ。上記の例では、工場の一部としてユーザーを作成するプロファイルがあります。プロファイル工場から* user *行を削除し、Userファクトリで関連付けを作成させることをお勧めします。 – Fred