2010-01-08 82 views
4

我测试如下:RSpec的测试关系逻辑

帐户

class Account < ActiveRecord::Base 
    has_many :ownerships 
    has_many :brands, :through => :ownerships 
end 

所有权加盟模式

class Ownership < ActiveRecord::Base 
    belongs_to :brand 
    belongs_to :account 
end 

测试

it "should be able to apply for brand ownership" do 
    account = Account.create(valid_account_attributes) 
    account.ownerships.create(:brand => Brand.create(:name => 'Superuser')) 
    account.ownerships.first.state == 'pending' 
end 

而且我不断收到此错误

You cannot call create unless the parent is saved 

我真的不明白 - 父母是什么?使用'create'方法时不应该创建和保存所有模型吗?我已经尝试过把'account.save'放在任何地方。

+0

我认为在某处存在验证错误。所以#save或#create返回false。我自己面临这个问题。在我的情况下,我是通过使用构建ie嘲笑返回类型即。不保存对象 – deepak 2012-09-25 12:55:12

回答

1

您确定account已被保存吗?您是否尝试使用create!来查看是否有任何例外情况发生?

0

我有同样的错误。我以为我已经删除了所有的表格,但仍然有一个用户,我试图用命令插入同一个用户。我通过擦除线来解决问题。