2013-04-04 100 views
0

我從Homepage01 HotDeal模型,它存在於ProductDetailController這樣Ruby on Rails的:如何驗證使用Rspec的或黃瓜

def Homepage01 
    @hotdetail=HotDeal.where("department_id=?",1).limit(15).offset(0) 
end 

我需要測試或使用Rspec的驗證數據查詢數據。我如何驗證RSpec或黃瓜框架中的數據或任何mysql錯誤?

+0

通過驗證它意味着什麼?模型的驗證也在測試環境中運行,並且mysql錯誤將被拋出並顯示在控制檯中(如果有的話)。你能否詳細說明你真正想要達到的目標? – weltschmerz 2013-04-04 11:20:52

+0

我將在控制器中創建一個HotDeal.where(「department_id =?」,1).limit(15).offset(0)的mysql查詢,因此對於此結果集,我必須測試是否得到了預期的結果或任何錯誤通過使用Ruby on rails測試框架 – user2244199 2013-04-04 12:02:28

回答

1

使用factory girldepartment_id 1創建一個hotdeal工廠。

裏面你product_detail_controller_spec.rb(這倒是應該是複數順便說一句,即product_details爲您的控制器和您的規格)寫:

require 'spec_helper' 

describe ClientsController do 
    describe "GET Homepage01" do 
    it "assigns the right records" do 
     FactoryGirl.create(:hot_deal) 
     hotdetail = HotDeal.where("department_id=?",1).limit(15).offset(0) 
     get "Homepage01" 
     assigns(:hotdetail).should eq(hotdetail) 
    end 
    end 
end 

這將確保您的查詢工作1)技術和2)如預期。

+0

指定(:hotdetail).should eq(hotdetail)沒有得到這一行,請你解釋一下嗎? – user2244199 2013-04-04 12:50:25

+0

'assigns(hotdetail)'確保你的控制器分配一個名爲'@ hotdetail'的實例變量,然後確保它等於'hotdetail',因爲這是你使用查詢的地方。 – weltschmerz 2013-04-04 13:52:37

+0

我跟着你的代碼,但得到# user2244199 2013-04-04 14:35:32