2011-08-26 48 views
3

我正在爲RESTfull JSON服務編寫驗收測試。我希望能夠對生產服務器運行測試。這個API被iphone客戶端使用。對於身份驗證,JSON服務使用Devise身份驗證令牌模塊。測試使用Devise令牌可驗證模塊和Cucumber的JSON API

下面是概括地說的協議:

iphone:返回200和下面的JSON {「令牌:POST/API/V1 /使用參數[email protected] &通= secretpass 服務器的令牌「:」UYUKJHBKHJJHSAD「} iphone:GET/api/v1/messages?auth_token = UYUKJHBKHJJHSAD

一切正常。

什麼是最好的方式來測試這與黃瓜?

我使用的是https://github.com/jayzes/cucumber-api-steps的api_steps,我一起砍了一些東西,這樣每個GET請求都會傳遞auth_token,但是這有點破解。

我所做的是創建以下的步驟:

我作爲用戶「[email protected]」,密碼爲「bingobingo」

我設置一個全局變量的auth_token該認證然後我追加到所有的GET請求。醜陋!

我在問你黃瓜/ Rails /測試大師!這樣做的最好方法是什麼?

回答

1

我發現RSpec比黃瓜更容易使用。

# spec/api/messages_spec.rb 
describe "messages", :type => :api do 
    it "retrieves messages" do 
    get "/api/v1/messages", :token => user.authentication_token 
    @messages = user.messages # this is the expected result 
    last_response.body.should eql @messages.to_json 
    last_response.status.should eql 200 
    end 
end 

# spec/support/api/helper.rb, so we have access to get, post, put, delete methods 
module ApiHelper 
    include Rack::Test::Methods 
    def app 
    Rails.application 
    end 
end 
RSpec.configure do |c| 
    c.include ApiHelper, :type => :api 
end 
相關問題