2010-01-08 45 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

我有同樣的錯誤。我以爲我已經刪除了所有的表格,但仍然有一個用戶,我試圖用命令插入同一個用戶。我通過擦除線來解決問題。