2013-02-02 30 views
0

我使用grape創建休息api我創建了API並且它的工作正常,現在我必須測試這個api.when我們創建rails api時會自動生成spec_helper.rb文件現在照常生成爲測試第一行是如何測試簡單的耙子應用程序

需要spec_helper

請告訴我應該是spec_helper.rb文件的代碼

和其他的東西測試一個簡單的耙application.i我給一個小的代碼時,我應該集中例如,我必須測試片段

require 'grape' 
require 'sequel' 
require 'json' 
module Twitter 
    class API < Grape::API 

    version 'v1', :using => :header, :vendor => 'twitter' 
    format :json 

    helpers do 
     def current_user 
     @current_user ||= User.authorize!(env) 
     end 

     def authenticate! 
     error!('401 Unauthorized', 401) unless current_user 
     end 
    end 

    resource :users do 



     desc "Return a status." 
     params do 
     requires :id, :type => Integer, :desc => "Status id." 
     optional :include , :type => String , :desc =>"parameter to include in " 

     end 
     get ':id' do 
"Hello World" 
end 
時,我稱這種葡萄應用

本地主機:9292 /用戶/ 1234 隨後的反應應該是「Hello World」的如何測試這個程序對於testing.i我只使用應該是什麼內容spec_helper.rb文件葡萄不使用導軌

+0

在一個目錄中我創建了一個api .api工作正常如何測試它 –

回答

0

這一切都取決於你想測試什麼。

假設你想測試的路由(localhost:9292/users/1234)是UsersController。既然如此,你會想要做這樣的事情(使用RSpec的):

describe UsersController do 
     context "GET#show" do 
     it "should return 'Hello World'" do 
      get :show, id: 1234 
      response.body.should include 'Hello World' 
     end 
     end 
    end 

現在作爲rake任務測試中,我將創建一個集成試驗,結果從命令行執行rake任務和比較預期的結果和排序任務的輸出結果如下:

describe "My Rake Task" do 
     it "should return hello world" do 
     results = `bundle exec rake my:rake:task` 
     results.should include 'Hello World' 
     end 
    end 

希望這些粗略的例子適合你!祝你好運!

UPDATE:

你應該總是寫上班級儘可能單元測試,以便您的rake任務測試是非常簡單的,甚至沒有必要的。

相關問題