我正在嘗試編寫一些rspec測試來檢查僅API應用程序的API端點。`get` API請求在Rspec測試中預期200錯誤
測試錯誤
Failure/Error: expect(res).to be_success
expected 200 to respond to `success?`
但是,如果同一呼叫(全文API URL)從另一個應用程序使其工作正常,並返回響應。從其他應用程序
例子:我想
res = RestClient.get "site.io/api/v1/projects/1"
p JSON.parse(res)
博客榜樣:(http://matthewlehner.net/rails-api-testing-guidelines/)。
# spec/requests/api/v1/messages_spec.rb
describe "Messages API" do
it 'sends a list of messages' do
FactoryGirl.create_list(:message, 10)
get '/api/v1/messages'
json = JSON.parse(response.body)
# test for the 200 status-code
expect(response).to be_success
# check to make sure the right amount of messages are returned
expect(json['messages'].length).to eq(10)
end
end
我的應用
/requests/projects_spec.rb
require 'rails_helper'
RSpec.describe Project do
describe "show_project" do
before do
@project1 = create(:project)
end
it "Checks if responds successfully" do
res = get '/api/v1/projects/1'
expect(res).to be_success
end
end
end
/factories/projects.rb
FactoryGirl.define do
factory :project do
name "Thing"
key "123123"
end
end
的routes.rb
namespace :api, :defaults => { :format => 'json'} do
namespace :v1 do
resources :projects, only: [:create, :show]
end
end
end
我沒有太多的測試經驗,所以如果任何人都可以指出我正確的方向,我真的很感激。