2013-02-02 24 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任务测试是非常简单的,甚至没有必要的。

相关问题