1

我写了一个端点,它有两个操作:PUT 和 GET。GET 检索 PUT 的内容,因此确保这两项工作的好方法是按顺序测试它们。

我的 cURL 测试告诉我该服务有效。但是,我无法通过做同样事情的测试!

it "accepts authenticated PUT data" do
    attributes1 = {
      user: {
        lat: '12.34',
        lng: '56.78'
      }
    }

    with_api(Location, { log_stdout: true, verbose: true }) do
      put_request({ query: { auth_token: 'abc' }, body: attributes1.to_json }) do |client|
        client.response_header.status.must_equal 201

        get_request({ query: { auth_token: 'abc' } }) do |client|
          client.response_header.status.must_equal 200
          client.response.must_match_json_expression([attributes1[:user]])
        end
      end
    end
end

请注意,PUT 在正文中接受 JSON,而 GET 返回 JSON。该服务使用 Redis 作为数据存储,我使用 mock_redis 模拟它。

test_helper.rb:

$redis = MockRedis.new

class Goliath::Server
  def load_config(file = nil)
    config['redis'] = $redis
  end
end

顶级规格:

before do
  $redis.flushdb
end

GET 应该检索刚刚 PUT 的内容,但它检索的是 JSON“null”。

我在做任何明显错误的事情吗?也许我如何将身体数据传递给请求?

当我的测试运行时,这肯定有助于获得更好的日志记录。我尝试了 { log_stdout: true, verbose: true } 选项,但这似乎仍然只从 Goliath 服务器输出基本的 INFO 日志记录,它没有告诉我有关数据和参数的任何信息。

任何帮助,将不胜感激!

4

0 回答 0