为什么我的规格生成的记录没有有效的ID值?

时间:2016-01-28 20:54:26

标签: ruby-on-rails ruby-on-rails-4 rspec rspec-rails

这是我初始化实例变量的方式:

context "when the inviter is being deleted and invited HAS accepted invitation" do
  before :each do
    @user1 = create(:user, gender: 0)
    @user2 = create(:user)
    @membership1 = create(:membership, member: nil, family_tree: @user1.family_tree, inviter: @user1, invited: @user2, relation: "sister", relative_type: 1)
    @membership2 = create(:membership, member: nil, family_tree: @user2.family_tree, inviter: @user2, invited: @user1, relation: "brother", relative_type: 1)
    @connection = create(:connection, inviter_membership: @membership1, invited_membership: @membership2, inviter_user: @user1, invited_user: @user2, request_status: 1)
    sign_in @user1
  end

然而,当我进行这个测试时:

  it "should NOT delete the inviter_membership record" do
    inviter_membership = @membership1
    delete :destroy, id: @user1
    expect(Membership.find(inviter_membership.id)).to be inviter_membership        
  end

我收到此错误:

ActiveRecord::RecordNotFound: Couldn't find Membership with 'id'=1441

当我在使用此测试失败生成的PRY控制台浏览我的数据库时,这些是我的结果:

[1] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> inviter_membership
=> #<Membership id: nil, family_tree_id: 31704, user_id: 13101, created_at: "2016-01-28 20:46:58", updated_at: "2016-01-28 20:46:58", relation: "sister", member_id: nil, invited_id: 13102, relative_type: 1>
[2] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @membership1
=> #<Membership id: nil, family_tree_id: 31704, user_id: 13101, created_at: "2016-01-28 20:46:58", updated_at: "2016-01-28 20:46:58", relation: "sister", member_id: nil, invited_id: 13102, relative_type: 1>

不是最初的membership.id: nil值。当我检查其他记录时,我看到类似的东西:

[5] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @user1
=> #<User id: nil, email: "eleanore.keeling@lindgrenhettinger.net", encrypted_password: "$2a$04$lROCfnS8rHf3Y2CKp0Ozw.9zsqa4rXTmxU6QvpGUm9S...", 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: "2016-01-28 20:46:58", updated_at: "2016-01-28 20:46:58", first_name: "Murphy", confirmation_token: nil, confirmed_at: "2016-01-28 20:46:56", confirmation_sent_at: nil, unconfirmed_email: nil, invitation_relation: "a", 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: "Facere consequatur odio recusandae aut ipsa repudi...", last_name: "Haag", gender: 0, birthday: nil>
[6] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @user2
=> #<User id: nil, email: "dillon.feest@jacobson.biz", encrypted_password: "$2a$04$ju0c8gr/RUaPeRNneh5rVOAN3QtUoMEzy9UwM4.a.D3...", 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: "2016-01-28 20:46:58", updated_at: "2016-01-28 20:46:58", first_name: "Neva", confirmation_token: nil, confirmed_at: "2016-01-28 20:46:56", confirmation_sent_at: nil, unconfirmed_email: nil, invitation_relation: "voluptatem", 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 optio mollitia et vel. Adipisci odit el...", last_name: "Rippin", gender: 1, birthday: nil>
[7] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @membership2
=> #<Membership id: nil, family_tree_id: 31706, user_id: 13102, created_at: "2016-01-28 20:46:58", updated_at: "2016-01-28 20:46:58", relation: "brother", member_id: nil, invited_id: 13101, relative_type: 1>

不确定是否与保存有关,但奇怪的是我的@connection值实际上有一个有效的ID:

[4] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @connection
=> #<Connection id: 1892, membership_id: 1441, sent_at: "2016-01-23 01:58:47", responded_at: "2016-02-12 11:17:20", send_limit: 5, times_sent: 1, removed_at: "2016-01-28 20:46:58", created_at: "2016-01-28 20:46:58", updated_at: "2016-01-28 20:46:58", request_status: 1, invited_membership_id: 1442, invited_user_id: 13102, inviter_user_id: 13101>

我唯一能想到的是,在我的代码中,我实际上在connection.update(...)的等效内容上调用了@connection。也许这个电话会保存记录吗?

如果是这种情况,我如何在Rspec中保存我的记录(我想要?)或如何在数据库中找到该记录?我需要验证它是否已被删除。

修改1

如果我在build初始化中使用@membership1而不是create并深入挖掘它,那就是我得到的:

[1] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @membership1
=> #<Membership id: nil, family_tree_id: 31799, user_id: 13146, created_at: "2016-01-28 21:08:09", updated_at: "2016-01-28 21:08:09", relation: "sister", member_id: nil, invited_id: 13147, relative_type: 1>
[2] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> inviter_membership
=> #<Membership id: nil, family_tree_id: 31799, user_id: 13146, created_at: "2016-01-28 21:08:09", updated_at: "2016-01-28 21:08:09", relation: "sister", member_id: nil, invited_id: 13147, relative_type: 1>
[3] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @membership1.valid?
=> true
[5] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @membership1.errors
=> #<ActiveModel::Errors:0x007fbd2f9fb458
 @base=
  #<Membership id: nil, family_tree_id: 31799, user_id: 13146, created_at: "2016-01-28 21:08:09", updated_at: "2016-01-28 21:08:09", relation: "sister", member_id: nil, invited_id: 13147, relative_type: 1>,
 @messages={}>

修改2

所以这是奇怪的事情,我在顶部的binding.pry中放置了一个before :each来检查在将执行发送到每个it块之前生成的值。那些值DO有ID。

  51:         @membership1 = build(:membership, member: nil, family_tree: @user1.family_tree, inviter: @user1, invited: @user2, relation: "sister", relative_type: 1)
    52:         @membership2 = create(:membership, member: nil, family_tree: @user2.family_tree, inviter: @user2, invited: @user1, relation: "brother", relative_type: 1)
    53:         @membership3 = build_stubbed(:membership)
    54:         @connection = create(:connection, inviter_membership: @membership1, invited_membership: @membership2, inviter_user: @user1, invited_user: @user2, request_status: 1)
    55:         sign_in @user1
 => 56:         binding.pry
    57:       end
    58: 
    59:       it "should confirm that inviter_membership has nil member_id" do
    60:         expect(@user1.inviter_memberships.first.member).to be nil
    61:       end

[1] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @membership1
=> #<Membership id: 1474, family_tree_id: 31878, user_id: 13183, created_at: "2016-01-28 21:37:38", updated_at: "2016-01-28 21:37:38", relation: "sister", member_id: nil, invited_id: 13184, relative_type: 1>
[2] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @membership2
=> #<Membership id: 1473, family_tree_id: 31880, user_id: 13184, created_at: "2016-01-28 21:37:38", updated_at: "2016-01-28 21:37:38", relation: "brother", member_id: nil, invited_id: 13183, relative_type: 1>
[3] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @membership3
=> #<Membership id: 1007, family_tree_id: 1001, user_id: 1003, created_at: nil, updated_at: nil, relation: "possimus", member_id: 1006, invited_id: 1005, relative_type: 0>

但是,在我关注的it块中,它似乎覆盖了每个实例变量的id值,除了我刚添加build_stubbed方法而不是{ {1}}:

create

什么可能会覆盖 71: inviter_membership = @membership1 72: delete :destroy, id: @user1 => 73: expect(Membership.find(inviter_membership.id)).to be inviter_membership 74: end 75: 76: it "should create a member record with inviter_user's info" do 77: expect { 78: delete :destroy, id: @user1 ActiveRecord::RecordNotFound: Couldn't find Membership with 'id'=1478 from /gems/activerecord-.1.14/lib/active_record/relation/finder_methods.rb:320:in `raise_record_not_found_exception!' [1] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @membership1 => #<Membership id: nil, family_tree_id: 31886, user_id: 13187, created_at: "2016-01-28 21:38:23", updated_at: "2016-01-28 21:38:23", relation: "sister", member_id: nil, invited_id: 13188, relative_type: 1> [2] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @membership2 => #<Membership id: nil, family_tree_id: 31888, user_id: 13188, created_at: "2016-01-28 21:38:23", updated_at: "2016-01-28 21:38:23", relation: "brother", member_id: nil, invited_id: 13187, relative_type: 1> [3] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @membership3 => #<Membership id: 1021, family_tree_id: 1015, user_id: 1017, created_at: nil, updated_at: nil, relation: "at", member_id: 1020, invited_id: 1019, relative_type: 0> 块中的id值?

编辑3

因此身份验证会发生奇怪的事情。

如果我it,那么现在失败的测试就会通过。

这是sign_in @user2失败的测试,但是传递了sign_in @user1

sign_in @user2

我尝试完全评论 it "should NOT delete the inviter_membership record" do expect { delete :destroy, id: @user1 }.not_to change(Membership,:count) end 行,这也导致上述测试通过。

奇怪的是,在我的sign_in模型上,我有user,当我在该方法中放置before_destroy :callback时,它不会在该测试中执行,因为失败并通过考试。所以似乎没有调用回调,这是非常奇怪的。

1 个答案:

答案 0 :(得分:0)

如果这是答案和正确的解释,我不是100%确定,但是Rspec向我投掷的错误最终得到了解决。

我相信正在发生的事情是before :each它成功创建并将对象保存到数据库中。但是,当规范失败时......它基本上构建了#39;对象并告诉我在规范失败后重建对象。这就是为什么我没有看到object.id的原因。

我相信是这样的,因为如果我在每个单元测试中放置一个不会失败的常规binding.pry,然后我查询before :each中初始化的实例变量,看起来很正常。例如:

  it "should NOT delete the inviter_membership record" do
    sign_in @user2
    binding.pry
    expect {
      delete :destroy, id: @user1
    }.not_to change(Membership,:count)
  end

结果如下:

[1] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @user1
=> #<User id: 13667, email: "cale@stehr.name", encrypted_password: "$2a$04$h.2yrLwRb1J1uJoWtFKUOuG.IW.UnchqXbfC7v.bJic...", 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: "2016-01-30 22:27:12", updated_at: "2016-01-30 22:27:12", first_name: "Daisy", confirmation_token: nil, confirmed_at: "2016-01-30 22:27:10", confirmation_sent_at: nil, unconfirmed_email: nil, invitation_relation: "vel", 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: "Maxime vitae dolorem atque perferendis sed fuga. A...", last_name: "Rohan", gender: 0, birthday: nil>
[2] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @user2
=> #<User id: 13668, email: "ashley_lockman@carroll.net", encrypted_password: "$2a$04$x3XvAGUAaxE0Kk8mtgcFCOb7FrGnPc3IS0wET/xauQU...", 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: "2016-01-30 22:27:12", updated_at: "2016-01-30 22:27:12", first_name: "Elliott", confirmation_token: nil, confirmed_at: "2016-01-30 22:27:10", confirmation_sent_at: nil, unconfirmed_email: nil, invitation_relation: "ducimus", 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: "Aperiam ut quia ea inventore non ad. Nobis nisi di...", last_name: "Hickle", gender: 1, birthday: nil>
[3] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @membership1
=> #<Membership id: 1747, family_tree_id: 32888, user_id: 13667, created_at: "2016-01-30 22:27:12", updated_at: "2016-01-30 22:27:12", relation: "sister", member_id: nil, invited_id: 13668, relative_type: 1>
[4] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @membership2
=> #<Membership id: 1748, family_tree_id: 32890, user_id: 13668, created_at: "2016-01-30 22:27:12", updated_at: "2016-01-30 22:27:12", relation: "brother", member_id: nil, invited_id: 13667, relative_type: 1>
[5] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @connection
=> #<Connection id: 2351, membership_id: 1747, sent_at: "2016-01-25 01:35:36", responded_at: "2016-02-07 13:16:04", send_limit: 5, times_sent: 1, removed_at: "2016-01-30 22:27:12", created_at: "2016-01-30 22:27:12", updated_at: "2016-01-30 22:27:12", request_status: 1, invited_membership_id: 1748, invited_user_id: 13668, inviter_user_id: 13667>

只有当规范失败时,它才会显示非持久化对象,如下所示:

RSpec::Expectations::ExpectationNotMetError: expected #count to have changed, but is still 2
from /gems/rspec-expectations-3.2.1/lib/rspec/expectations/fail_with.rb:29:in `fail_with'
[1] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @user1
=> #<User id: nil, email: "maurine.abbott@heaney.name", encrypted_password: "$2a$04$9AJpP7/UwUDoYMw/YrSd2O2qy.nSZguZGnlWyNt8mVV...", 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: "2016-01-30 22:28:22", updated_at: "2016-01-30 22:28:22", first_name: "Ashtyn", confirmation_token: nil, confirmed_at: "2016-01-30 22:28:20", confirmation_sent_at: nil, unconfirmed_email: nil, invitation_relation: "doloribus", 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: "Perspiciatis dolor natus ut incidunt aperiam culpa...", last_name: "Jones", gender: 0, birthday: nil>
[2] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @user2
=> #<User id: nil, email: "scotty.dooley@metz.name", encrypted_password: "$2a$04$ReYuDORNmKSNeDEYPZrmwuFHPfkv2JWypPQjmKuQVuy...", 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: "2016-01-30 22:28:22", updated_at: "2016-01-30 22:28:22", first_name: "Albin", confirmation_token: nil, confirmed_at: "2016-01-30 22:28:20", confirmation_sent_at: nil, unconfirmed_email: nil, invitation_relation: "dolores", 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 soluta dolorum harum rerum fuga repudiandae....", last_name: "Dooley", gender: 1, birthday: nil>
[3] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @membership1
=> #<Membership id: nil, family_tree_id: 32975, user_id: 13708, created_at: "2016-01-30 22:28:22", updated_at: "2016-01-30 22:28:22", relation: "sister", member_id: nil, invited_id: 13709, relative_type: 1>
[4] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @membership2
=> #<Membership id: nil, family_tree_id: 32977, user_id: 13709, created_at: "2016-01-30 22:28:22", updated_at: "2016-01-30 22:28:22", relation: "brother", member_id: nil, invited_id: 13708, relative_type: 1>
[5] pry(#<RSpec::ExampleGroups::UsersController::DELETEDestroy::WhenTheInviterIsBeingDeletedAndInvitedHASAcceptedInvitation>)> @connection
=> #<Connection id: 2375, membership_id: 1763, sent_at: "2016-01-25 00:35:20", responded_at: "2016-02-20 13:06:58", send_limit: 5, times_sent: 1, removed_at: "2016-01-30 22:28:22", created_at: "2016-01-30 22:28:22", updated_at: "2016-01-30 22:28:22", request_status: 1, invited_membership_id: 1764, invited_user_id: 13709, inviter_user_id: 13708>

我仍然不明白为什么最后一个实例变量 - @connection - 是正确持久的,而其他变量不是......唉......它现在适合我。

我希望这有助于某人!