2011-10-05 74 views
1

我正在写一些规范,下面的失败,但页面/ menus/1在浏览器中加载罚款。这是一个php应用程序的端口,并且是我第一次使用RSpec。任何想法,为什么它可能不工作。Rails 3.1,RSpec - 失败,但页面加载正常

错误是:

1) MenusController GET 'show' should be succesful 
    Failure/Error: get :show, :id => 1 
    ActiveRecord::RecordNotFound: 
    Couldn't find MenuHeader with id=1 
    # ./app/controllers/menus_controller.rb:18:in `show' 
    # ./spec/controllers/menus_controller_spec.rb:7:in `block (3 levels) in <top (required)>' 

但具体MenuHeader并基于所有正常标准(控制台,MySQL和浏览器)存在。我敢肯定,99%我有一个错误在我的规格:

require 'spec_helper' 

describe MenusController do 
    describe "GET 'show'" do 
    it "should be succesful" do 
     get :show, :id => 1 
     response.should be_success 
    end 
    end 
end 

这里是menus_controller.rb

def show 
    @menu_header_data=MenuHeader.find(params[:id]) 


    respond_to do |format| 
    format.html # show.html.erb 
    # format.json { render json: @menu } to do 
    end 
end 

THX

+0

另外:你有一个测试“获取显示与不存在的ID应该失败”? – Zabba

+0

现在正在处理它 – timpone

+0

您是否正在对测试数据库运行此操作?你在使用固定装置或工厂吗? – zetetic

回答

4

当测试使用RSpec或TestUnit我会用一个控制器一个工厂或夹具来传递id而不是用数据建立一个测试数据库。这是更好的东西,如测试:

使用FactoryGirl(我的建议,但每个人都有自己的口味):

describe MenusController do 
    describe "GET 'show'" do 
    it "should be succesful" do 
     get :show, :id => Factory(:menu).id 
     response.should be_success 
    end 
    end 
end 

的测试主要就是为了确保提供有效的数据,当控制器正确响应,并使用工厂或夹具不易碎。如果维护测试套件是基于硬件数据(如Fixtures或数据库备份),那么维护测试套件将会变得非常痛苦,并最终导致您放弃测试驱动开发而不是拥抱测试套件。

+0

'Factory.define:menu do | menu | menu.name“我的菜单名称” menu.id 1 end' – timpone