rails 4 rspec 曾经工作,现在失败了 user.profile 是零
rails 4 rspec use to work and now it fails user.profile is nil
这个测试曾经有效,但我不明白为什么停止了。现在当我 运行 它 other_user.profile 等于 nil.
测试:
require 'rails_helper'
describe "User" do
describe "abilities" do
subject(:ability){ Ability.new(user) }
let(:other_user) { nil }
context "not logged in (Guest)" do
let(:other_user){ build(:user_with_profile) }
it{ should be_able_to(:show, other_user.profile) }
end
end
end
工厂:
# users.rb factory
FactoryGirl.define do
factory :user do
email { Faker::Internet.email }
password "secret"
end
factory :user_with_profile, parent: :user do
after(:build) do |user|
build(:profile, user: user)
end
end
# profiles.rb factory
include ActionDispatch::TestProcess
FactoryGirl.define do
factory :profile do |f|
association :user, factory: :user
f.about { Faker::Lorem.paragraph(2) }
f.avatar { fixture_file_upload(Rails.root.join('spec', 'support', 'test.png'), 'image/png') }
end
end
我在这里错过了什么?
尝试改变
factory :user_with_profile, parent: :user do
after(:build) do |user|
build(:profile, user: user)
end
end
至
factory :user_with_profile, parent: :user do
after(:build) do |user|
user.profile = build(:profile)
end
end
这个测试曾经有效,但我不明白为什么停止了。现在当我 运行 它 other_user.profile 等于 nil.
测试:
require 'rails_helper'
describe "User" do
describe "abilities" do
subject(:ability){ Ability.new(user) }
let(:other_user) { nil }
context "not logged in (Guest)" do
let(:other_user){ build(:user_with_profile) }
it{ should be_able_to(:show, other_user.profile) }
end
end
end
工厂:
# users.rb factory
FactoryGirl.define do
factory :user do
email { Faker::Internet.email }
password "secret"
end
factory :user_with_profile, parent: :user do
after(:build) do |user|
build(:profile, user: user)
end
end
# profiles.rb factory
include ActionDispatch::TestProcess
FactoryGirl.define do
factory :profile do |f|
association :user, factory: :user
f.about { Faker::Lorem.paragraph(2) }
f.avatar { fixture_file_upload(Rails.root.join('spec', 'support', 'test.png'), 'image/png') }
end
end
我在这里错过了什么?
尝试改变
factory :user_with_profile, parent: :user do
after(:build) do |user|
build(:profile, user: user)
end
end
至
factory :user_with_profile, parent: :user do
after(:build) do |user|
user.profile = build(:profile)
end
end