我正在使用RSpec和Factory Girl。
以下是我在高层的协会:
User
has_one :family_tree, dependent: :destroy
has_many :memberships, through: :family_tree, dependent: :destroy
Family Tree
belongs_to :user
has_many :memberships, dependent: :destroy
has_many :members, through: :memberships, dependent: :destroy
Membership
belongs_to :family_tree
belongs_to :user
belongs_to :member
Member
has_many :memberships, dependent: :destroy
这是my_relatives
课程中User
的方法:
def my_relatives
self.family_tree.memberships.pluck(:relation)
end
这些是我的工厂:
用户
factory :user do
association :family_tree
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
email { Faker::Internet.email }
password "password123"
password_confirmation "password123"
bio { Faker::Lorem.paragraph }
invitation_relation { Faker::Lorem.word }
# required if the Devise Confirmable module is used
confirmed_at Time.now
gender 1
end
会员
factory :member do
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
email { Faker::Internet.email }
bio { Faker::Lorem.paragraph }
gender 1
end
成员
factory :membership do
family_tree
user
relation { Faker::Lorem.word }
member
end
家谱
factory :family_tree do
name { Faker::Name.name }
end
我的测试:
it "should return all the relatives of a user" do
u1 = create(:user)
ft = create(:family_tree, user: u1)
relation = "nephew"
u2 = create(:user)
membership = create(:membership, user: u2, relation: relation, family_tree: ft)
expect(u1.my_relatives).to eq(u2.memberships.where(relation: relation).pluck(:relation))
end
当我运行RSpec时,这是我得到的错误:
1) User should return all the relatives of a user
Failure/Error: expect(u1.my_relatives).to eq(u2.memberships.where(relation: relation).pluck(:relation))
expected: []
got: ["nephew"]
(compared using ==)
为什么我得到一个空数组作为u1.my_relatives
的主要期望?
当我在运行时使用pry浏览变量时,我得到了这个:
1] pry(#<RSpec::ExampleGroups::User>)> u1
=> #<User id: nil, email: "felicita@adams.info", encrypted_password: "$2a$04$EJWX5sLmBtDBgyA8tf35y.1FfhQcLRY3EAU/JxL8/tN...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2015-10-26 23:15:30", updated_at: "2015-10-26 23:15:30", first_name: "Brooks", confirmation_token: nil, confirmed_at: "2015-10-26 23:15:27", confirmation_sent_at: nil, unconfirmed_email: nil, invitation_relation: "similique", avatar: nil, invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_id: nil, invited_by_type: nil, invitations_count: 0, bio: "Reiciendis ea omnis. Saepe ea amet nostrum deserun...", last_name: "Yost", gender: 1>
[2] pry(#<RSpec::ExampleGroups::User>)> ft
=> #<FamilyTree id: nil, name: "Ms. Milan Goldner", user_id: 1197, created_at: "2015-10-26 23:15:30", updated_at: "2015-10-26 23:15:30">
[3] pry(#<RSpec::ExampleGroups::User>)> relation
=> "nephew"
[4] pry(#<RSpec::ExampleGroups::User>)> u2
=> #<User id: nil, email: "jeie@lockman.org", encrypted_password: "$2a$04$z.UVWka/mRKCK6hp33LOuuBHaL8S7h1LnmWbonMfBkd...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2015-10-26 23:15:30", updated_at: "2015-10-26 23:15:30", first_name: "Adrien", confirmation_token: nil, confirmed_at: "2015-10-26 23:15:27", confirmation_sent_at: nil, unconfirmed_email: nil, invitation_relation: "repellat", avatar: nil, invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_id: nil, invited_by_type: nil, invitations_count: 0, bio: "Rerum sunt ratione labore deserunt. Et quibusdam s...", last_name: "Renner", gender: 1>
[5] pry(#<RSpec::ExampleGroups::User>)> membership
=> #<Membership id: 126, family_tree_id: 1649, user_id: 1198, created_at: "2015-10-26 23:15:30", updated_at: "2015-10-26 23:15:30", relation: "nephew", member_id: 75, connection_sent_at: nil, connection_responded_at: nil, connect_send_limit: nil, connect_times_sent: nil, connected: nil, connect_type: nil, request_status: nil, connection_removed_at: nil>
[6] pry(#<RSpec::ExampleGroups::User>)> u1.my_relatives
=> []
[7] pry(#<RSpec::ExampleGroups::User>)> u1.family_tree
=> #<FamilyTree id: nil, name: "Ms. Milan Goldner", user_id: 1197, created_at: "2015-10-26 23:15:30", updated_at: "2015-10-26 23:15:30">
[8] pry(#<RSpec::ExampleGroups::User>)> u1.family_tree.memberships
=> []
[9] pry(#<RSpec::ExampleGroups::User>)> u1.memberships
=> []
[11] pry(#<RSpec::ExampleGroups::User>)> membership
=> #<Membership id: 126, family_tree_id: 1649, user_id: 1198, created_at: "2015-10-26 23:15:30", updated_at: "2015-10-26 23:15:30", relation: "nephew", member_id: 75, connection_sent_at: nil, connection_responded_at: nil, connect_send_limit: nil, connect_times_sent: nil, connected: nil, connect_type: nil, request_status: nil, connection_removed_at: nil>
[12] pry(#<RSpec::ExampleGroups::User>)> membership.family_tree
=> #<FamilyTree id: nil, name: "Ms. Milan Goldner", user_id: 1197, created_at: "2015-10-26 23:15:30", updated_at: "2015-10-26 23:15:30">
因此,即使正确分配了本地变量membership
,即使用正确的user
和family_tree
记录,当我尝试从用户的角度访问它时 - 即u1
它返回一个空数组。
我认为这与我family_tree
对象上的nil
记录为u1
这一事实有关。不知道怎么处理那个。
答案 0 :(得分:1)
我认为您不需要使用through: :family_tree, dependent: :destroy
在
用户
has_one :family_tree, dependent: :destroy
has_many :memberships, through: :family_tree, dependent: :destroy
因为您在会员资格中有belongs_to :user
会员
belongs_to :family_tree
belongs_to :user
belongs_to :member
当您执行u2.memberships.where(relation: relation).pluck(:relation)
时,您的测试正在生成[]数组,因为没有为第二个用户创建族谱。