我正在尝试测试我的 Grape API,但我在测试中收到 400 错误,但是当我运行测试应该测试的操作时,我得到了预期的 201 HTTP 响应。不知道这里发生了什么。下面是具体的 RSpec 测试,但您可以在 GitHub 上的hackcentral/hackcentral查看整个项目以及工厂和实际的 Grape API 。下面的测试是测试 Alpha::Applications 上的 POST 创建操作。(app/api/alpha/applications.rb)
describe 'POST #create' do
before :each do
@oauth_application = FactoryGirl.build(:oauth_application)
@token = Doorkeeper::AccessToken.create!(:application_id => @oauth_application.id, :resource_owner_id => user.id)
end
context "with valid attributes" do
it "creates a new application" do
expect{
post "http://api.vcap.me:3000/v1/applications?access_token=#{@token.token}", application: FactoryGirl.attributes_for(:application), :format => :json
} .to change(Application, :count).by(1)
end
it "creates a new application, making sure response is #201" do
post "http://api.vcap.me:3000/v1/applications", application: FactoryGirl.attributes_for(:application), :format => :json, :access_token => @token.token
response.status.should eq(201)
end
end
end