【问题标题】:Grape Rails RSpec test returning 400Grape Rails RSpec 测试返回 400
【发布时间】:2015-04-08 10:31:02
【问题描述】:

我正在尝试测试我的 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

【问题讨论】:

    标签: ruby-on-rails rspec grape grape-api grape-entity


    【解决方案1】:

    我不明白你为什么要测试 http://api.vcap.me 而不是 localhost?

    您通常在本地环境中测试应用。这也不是测试服务器是否正常工作的正确原因。

    下面是一个示例,说明您的测试应该是什么样子。 https://github.com/dblock/grape-on-rails/blob/master/spec/api/ping_spec.rb 来自 example project

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      相关资源
      最近更新 更多