Javascript 如何使用Sinon.JS为服务器响应计时?
我想测试我的web应用程序如何处理服务器响应。这就是为什么我创建了一个使用 我的应用程序代码发出两个请求,在我的测试场景中,我希望强制在对第二个请求的响应之后发送对第一个请求的响应 顺序:Javascript 如何使用Sinon.JS为服务器响应计时?,javascript,mocking,jasmine,integration-testing,sinon,Javascript,Mocking,Jasmine,Integration Testing,Sinon,我想测试我的web应用程序如何处理服务器响应。这就是为什么我创建了一个使用 我的应用程序代码发出两个请求,在我的测试场景中,我希望强制在对第二个请求的响应之后发送对第一个请求的响应 顺序: 请求1 请求2 答复2 答复1 以下是我为测试用例编写的CoffeeScript代码: # Request 1 server.respondWith 'GET', "http://localhost/endpoint", [200, {"Content-Type": "application/json"},
# 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()
一旦启动测试,所有REST调用都将调用http://localhost/endpoint
从我的应用程序代码中获得相同的响应({“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()
为此,您可以使用Pivotal制作的lib
咖啡脚本:
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()
可以按时间排序所有请求,并将它们放入一个数组中,例如,您可以移动请求并响应它们
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