2013-08-16 132 views
0

我正在使用Devise进行我的用户身份验证,并且想要与用户一起销毁关联的配置文件。has_one,dependent:destroy not working

我失败的规格如下所示:

it "should destroy associated profile" do 
    profile = @user.profile 
    @user.destroy 
    expect(profile).to be_nil 
end 

而且

在我的用户模型:

has_one :profile, dependent: :destroy 

在我的剖面模型:

belongs_to :user 

在控制台,我可以重现像这样的问题:

2.0.0p247 :001 > @user = FactoryGirl.create(:user) 
    (1.5ms) BEGIN 
    User Exists (2.9ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 
    User Exists (1.7ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1 
    SQL (15.7ms) INSERT INTO "users" ("created_at", "email", "encrypted_password", "name", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00], ["email", "[email protected]"], ["encrypted_password", "$2a$10$0704XOlw.6ZE4HEfDhaIeuwnEbbJZvZda3Jwr052aLS5z3G77Dgja"], ["name", "Example User"], ["updated_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00]] 
    SQL (3.8ms) INSERT INTO "profiles" ("created_at", "updated_at", "user_id") VALUES ($1, $2, $3) RETURNING "id" [["created_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00], ["updated_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00], ["user_id", 25]] 
    Profile Load (3.4ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 ORDER BY "profiles"."id" ASC LIMIT 1 [["user_id", 25]] 
    (2.2ms) COMMIT 
=> #<User id: 25, email: "[email protected]", encrypted_password: "$2a$10$0704XOlw.6ZE4HEfDhaIeuwnEbbJZvZda3Jwr052aLS5...", 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: "2013-08-16 01:21:12", updated_at: "2013-08-16 01:21:12", name: "Example User"> 
2.0.0p247 :002 > @user.destroy 
    (1.0ms) BEGIN 
    SQL (2.5ms) DELETE FROM "profiles" WHERE "profiles"."id" = $1 [["id", 4]] 
    SQL (5.4ms) DELETE FROM "users" WHERE "users"."id" = $1 [["id", 25]] 
    (2.0ms) COMMIT 
=> #<User id: 25, email: "[email protected]", encrypted_password: "$2a$10$0704XOlw.6ZE4HEfDhaIeuwnEbbJZvZda3Jwr052aLS5...", 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: "2013-08-16 01:21:12", updated_at: "2013-08-16 01:21:12", name: "Example User"> 

有趣的是,用户似乎实际上已被删除。

2.0.0p247 :003 > @user.reload.destroy 
    User Load (2.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 25]] 
ActiveRecord::RecordNotFound: Couldn't find User with id=25 

这是怎么回事?

回答

0

重写我的规格是这样做什么,我需要:

it "should destroy associated profile" do 
     expect { 
     @user.destroy 
     }.to change(Profile, :count).by(-1) 
    end 
1

这个怎么样?

it "should destroy associated profile" do 
    profile = @user.profile 
    @user.destroy 
    expect(@user.profile).to be_nil 
end 

@ user.destroy之后,预期为零的'profile'变量没有改变。我认为...

3

你的模型看起来不错。尝试是这样的:

it "should destroy associated profile" do 
    profile = @user.profile 
    @user.destroy 
    expect(Profile.find(profile.id)).to be_nil 
end 

像Heungju说,而对应于profile数据库行被销毁,变量本身是没有的。

+0

谢谢你,这确实是我跑成问题。这样的数据库查询返回一个错误,而不是零,所以我重写了规范。我正在提交这个重写作为答案。 – mpgarate