并行测试中MockServer的Cookie使用情况

时间:2018-03-22 07:30:04

标签: ruby-on-rails ruby automated-tests mockserver

我正在尝试并行运行多个测试,我需要为通过MockServer发出的每个请求获取独立的响应。

我正在使用gem https://github.com/jamesdbloom/mockserver-client-ruby来获取以下请求:

def mock_successful_transaction_request(successful = true)
    client = initialize_mock
    expectation = expectation do |e|
      e.request do |request|
        request.method = 'POST'
        request.path = '/transactions'
        request.cookies << cookie('sessionId', SecureRandom.hex)
      end

      dummy_response = mock_tb_response(successful)

      e.response do |response|
        response.status_code = 201
        response.body = dummy_response.to_json
      end
    end

    # puts expectation.to_yaml
    client.register(expectation)
 end

def initialize_mock
    client = MockServerClient.new(MOCK_SERVER[:host], MOCK_SERVER[:port])
    client.logger = Logger.new("mockserver_logs.log")
    client
  end

def mock_tb_response(successful)
    tb_id = Faker::Number.between(1, 1_000_000)
    successful ? { id: tb_id, v: '2.0.0' } : {}
  end

当我添加request.cookies << cookie("sessionId", SecureRandom.hex)部分时,我似乎无法获得响应,并且我的请求因HTTP 500而失败。如果我将其删除,则响应按预期进行。

我做错了什么?

谢谢。

1 个答案:

答案 0 :(得分:0)

尝试:     request.cookies [:sessionId] = cookie('sessionId',SecureRandom.hex)

或者:     request.cookies [:sessionId] = SecureRandom.hex

<强>更新

尝试:

request.cookies = [cookie('sessionId', '2By8LOhBmaW5nZXJwcmludCIlMDAzMW')]

如果有效,请尝试用SecureRandom.hex替换'2By8LOhBmaW5nZXJwcmludCIlMDAzMW'