2013-04-08 31 views
1

我在名爲HomePage的該方法中有一個名爲productscontroller的控制器。測試我的Ruby on Rails應用程序

代碼如下:

class ProductsController < ApplicationController 
     def HomePage 
     end 
    end 

在routes.rb中文件

root :to => 'products#HomePage'

所以,當我點擊localhost:3000它呈現網頁。

對於這個HomePage.html.erb文件,我打電話給javascript文件,在這個javascript文件中發送另一個ajax請求來渲染中心內容。 Path: 'homepage01/1'這個要求,我有行動,像

def homepage01 
"query executed and stored in instance variable" 
end 

控制器所以我的問題是如何測試這個頁面是否被渲染成功或測試framewoks任何錯誤等?

回答

0

這是一個與rspec的簡單: 創建在投機/控制器/ products_controller_spec.rb試驗用下面的代碼:

describe ProductsController do 
    describe '#homepage01' do 
    before { get :homepage01 } 

    it { should respond_with(:success) } 
    it { should render_template(:template_name) } 
end 
+0

沒有路由匹配{:控制器=> 「產品」,:動作=> 「homepage01」}我已經包含組:測試:發展做 寶石「RSpec的護欄」 寶石「黃瓜軌」 寶石「水豚」 寶石「database_cleaner」 寶石「factory_girl_rails」,「〜> 4.0」 結束寶石文件。我應該錯過什麼嗎? – user2244199 2013-04-08 10:55:33

+0

通過調用'rake routes'查看您的路線 - 這將顯示您應用中的所有可用路徑 - 控制器和操作。 我想你的routes.rb中有這個錯誤原因,這個路徑沒有定義。 嘗試添加水木清華這樣 資源:產品:PATH => '',:只=> [:首頁,:homepage01] 獲得的產品/ homepage01「 獲得 '產品/主頁' 但這名是非常糟糕的,你應該使用簡單的REST和CRUD操作 – Extazystas 2013-04-08 11:53:13

+0

我通過使用訪問root_path來修復路由錯誤,但現在正在獲取失敗/錯誤:{應該respond_with(:success)} NoMethodError: 未定義的方法'respond_with'for# RSpec :: Core :: ExampleGroup :: Nested_1 :: Nested_1:0xb94c9a0> – user2244199 2013-04-08 12:23:36