在测试中重新加载记录时,关联会清除

时间:2013-02-07 04:34:36

标签: ruby-on-rails testunit

我只熟悉Rspec,但现在我需要修复一个使用Test :: Unit的库。

我发现当我重新加载记录时,关联变为零

例如:

test "accepts nested attributes from subscription to user ()" do
  @user = User.create(name:'Ash')
  @subscription = @user.create_subscription()
  puts "user: " << @user.inspect
  puts "subscription: " << @user.subscription.inspect

  @subscription.update_attributes({ :user_attributes => { :notification_comment => true } })
  @user.reload
  puts "user: " << @user.inspect
  puts "subscription: " << @user.subscription.inspect
  assert @user.subscription, "should have an subscription"
end

输出:

user: #<User id: 815823837, account_id: nil, name: "Ash", email: nil, settings: {}, created_at: "2013-02-07 04:28:51", updated_at: "2013-02-07 04:28:51">
subscription: #<Subscription id: 1, user_id: 815823837, created_at: "2013-02-07 04:28:51", updated_at: "2013-02-07 04:28:51">
user: #<User id: 815823837, account_id: nil, name: "Ash", email: nil, settings: {}, created_at: "2013-02-07 04:28:51", updated_at: "2013-02-07 04:28:51">
subscription: nil

为什么会这样?我做了检查,我可以确认我可以Subscription.find(@subscription.id),以便将其保存在数据库中。

0 个答案:

没有答案