2013-04-04 98 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