如何使用Sinon.JS计算服务器响应时间?

时间:2015-08-13 13:22:01

标签: javascript mocking jasmine integration-testing sinon

我想测试一下我的Web应用程序如何处理服务器响应。这就是为什么我创建了一个使用Sinon.JS to fake a server的测试场景。

我的应用程序代码发出两个请求,在我的测试场景中,我想强制第一个请求的响应在响应第二个请求之后发送。

序列:

  1. 请求1
  2. 请求2
  3. 回复2
  4. 回复1
  5. 以下是我为测试用例编写的CoffeeScript代码:

    # Request 1
    server.respondWith 'GET', "http://localhost/endpoint", [200, {"Content-Type": "application/json"}, '{"A":"A"}']
    # Request 2
    server.respondWith 'GET', "http://localhost/endpoint", [200, {"Content-Type": "application/json"}, '{"B":"B"}']
    
    # My application code
    ...
    
    # Response 1
    server.respond()
    
    # Response 2
    server.respond()
    

    一旦我开始测试,我的应用程序代码中对http://localhost/endpoint的所有REST调用都得到相同的响应({"B":"B"})。所以对我来说,看起来像Sinon.JS始终使用respondWith定义的最后一个URL映射的响应。

    但我希望我的假冒服务器将{"B":"B"}返回http://localhost/endpoint的第一个点击。在第二次点击时,它应该返回{"A":"A"}

    是否可以做这样的事情?

    # Request 1
    request_1 = server.respondWith 'GET', "http://localhost/endpoint", [200, {"Content-Type": "application/json"}, '{"A":"A"}']
    # Request 2
    request_2 = server.respondWith 'GET', "http://localhost/endpoint", [200, {"Content-Type": "application/json"}, '{"B":"B"}']
    
    # My application code (makes multiple requests to the same endpoint)
    ...
    
    # Response 1
    request_2.respond()
    
    # Response 2
    request_1.respond()
    

2 个答案:

答案 0 :(得分:2)

您可以使用Pivotal为此创建的Jasmine-AJAX lib。

CoffeeScript的:

it 'can handle an unlimited amount of requests and respond to each one individually after all requests have been made', ->
    jasmine.Ajax.install() # put this in beforeEach
    url = 'http://localhost/test'

    $.ajax
            dataType: 'json'
            url: url
            success: (data, textStatus, jqXHR) ->
                    # Receives {"A":"A"}
                    console.log "Response: #{JSON.stringify(data)}"

    $.ajax
            dataType: 'json'
            url: url
            success: (data, textStatus, jqXHR) ->
                    # Receives {"B":"B"}
                    console.log "Response: #{JSON.stringify(data)}"

    responses = [
            {
                    contentType: 'application/json'
                    responseText: '{"A":"A"}'
                    status: 200
            },
            {
                    contentType: 'application/json'
                    responseText: '{"B":"B"}'
                    status: 200
            }
    ]

    for i in [0...jasmine.Ajax.requests.count()]
            request = jasmine.Ajax.requests.at i
            request.respondWith responses[i]

    expect(jasmine.Ajax.requests.count()).toBe 2
    jasmine.Ajax.uninstall() # put this in afterEach

使用count()at(),您可以按时间排序所有请求并将它们放在一个数组中,您可以在其中移动请求并响应它们。

答案 1 :(得分:0)

var count = 0

$.mockjax(
  function(requestSettings){
    if(requestSettings.url === "path/to/api" && requestSettings.type === "POST"){

      return {
        response: function(origSettings){
          if (count == 0){
            this.responseText = {msg:"this is first response" }
            this.status = 200
          }
          else if(count == 1){
            //more combos
          }
          else{
            this.responseText = {msg:"this is last response" }
            this.status = 200
          }
          count = (count + 1) % 4
        }
      }
    }
  }
})