나는 내 질문에 대한 Google과 stackoverflow를 탐색하고 비슷한 문제를 발견했지만 아무도 내 문제를 해결하지 못했습니다.FactoryGirl 및 Rspec과 has_one 연관의 하위 객체를 만드는 방법은 무엇입니까?
내 앱에서 사용자 has_one 프로필과 프로필 belongs_to User.
일부 사용자 기능을 테스트하려고하는데 제대로 수행하려면 테스트 사용자와 관련된 테스트 프로필을 만들어야합니다. 여기
내 공장/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
기존 사용자의 시나리오 기호는 관련 프로필이 필요한 곳입니다. 이자형.
는 지금은 그냥이 프로필을 연결하는 블록을 만들 통과 시도
let(:profile) {create(:profile, user: user)}
공장을 사용하여 프로파일을 작성하고, 나는 그것을 연결하는 프로파일의 USER_ID 속성을 재정의하는 시도했습니다 생성 된 사용자는 아니지만 둘 다 성공했습니다. 이상적으로는 사용자를 만들 때마다 관련 프로필이 만들어 지도록 설정하고 싶습니다. 어떤 아이디어?
나는 이것이 너무 어려울 수 없다는 것을 안다. 나는 해결책을 고안하지 못했다. 도와 주셔서 감사합니다.
이것이 내 문제를 해결하지만 지금은 stack_ level이 너무 커서 내 user_factory에서이 줄을 가리키는 오류가 발생했다고 생각합니다. 이메일 {Faker :: Internet.safe_email}이 문제의 원인은 무엇입니까? – srlrs20020
아. 위의 예는 공장의 일부로 사용자를 생성하는 프로필을 가지고 있습니다. 프로필 팩토리에서 * 사용자 * 줄을 제거하고 사용자 팩터 리에 연결을 만들도록 제안합니다. – Fred