2011-10-05 58 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

相關問題