0
我after_create方法如下:如何測試rspec中的after_create方法?
company.rb
class Company < ActiveRecord::Base
after_create :create_subscriptions
def create_subscriptions
subscription=Subscription.create(:company_id => self.id, :subscription_dt => Date.today, :is_active => 'Y', :last_renewal_dt => Date.today + 2,:user_id => self.users.first.id)
subscription.save
end
end
雖然我創建了一家名爲after_create方法和認購表格輸入數據。
在rspec中,我創建了公司並完全創建了它的成功。但如何測試「create_subscriptions」方法? whoch在創建之後調用。我可以在rspec代碼中進行查詢嗎?像
rspec的代碼:
@company = create(:company)
@sub = Subscription.find(:first,:conditions => ['company_id= ?', @company.id])
expect(@sub.company_id).should eq(@company.id)
是它確定?我在谷歌搜索中沒有在rspec代碼中看到這種類型的查詢。在這裏使用存根或模擬?
任何人都可以請指導我嗎?我想我必須使用存根和模擬,但我不知道如何使用它們?
感謝,
謝謝,所以你的意思是我們可以使用查詢來獲取測試數據d比較它在rspec中。但是大多數rspec示例中我沒有發現從數據庫獲取數據,所以我有一個疑問。但謝謝你的幫助。我修改了以下代碼: subscription = @ company.subscription expect(subscription.company_id).to eq(@ company.id) –