sinon模拟GET,POST,PUT和DELETE方法断言每次都会失败。不知道为什么

时间:2018-08-09 06:49:41

标签: node.js sinon nodeunit

上午学习sinon和nodejs的nodeUnit。编写了简单的module来处理GET,POST,PUT和DELETE方法。

然后开始编写单元测试。现在,我的问题有两个方面:

一个存在:在GET test中,当我指定requestMock.method = 'GET';时, 运行nodeunit test-http-module.js测试失败,当检查调用堆栈时,它说POST请求失败(handlePostRequest如下面的调用堆栈中突出显示)。

两个存在:将requestMock.method = 'GET';更改为'POST', 'PUT' or 'DELETE'时,无论我进行什么更改,测试仍然失败!但在后一种情况下,调用堆栈会显示定向到的正确方法。

我认为只有一个问题会导致连续失败,并且GET被引向POST

请解释出什么问题。

  

ExpectationError:意外调用:writeHead(200,{Content-Type:   “ text / plain”})

     

符合预期:writeHead(200,{Content-Type:“ text / plain”} [,...])一次

     

在Object.fail(D:\ Projects \ node-book \ node_modules \ sinon \ lib \ sinon \ mock-expectation.js:281:25)

     

在Object.invokeMethod(D:\ Projects \ node-book \ node_modules \ sinon \ lib \ sinon \ mock.js:174:25)

     

在Object.writeHead(D:\ Projects \ node-book \ node_modules \ sinon \ lib \ sinon \ mock.js:67:35)

     

handlePostRequest(D:\ Projects \ node-book \ modules \ http-module.js:8:14)

     

在Object.handleRequest(D:\ Projects \ node-book \ modules \ http-module.js:34:26)

     

在Object.exports.handleGetRequestTest(D:\ Projects \ node-book \ unit-testing-intro \ test-http-module.js:22:21)

1 个答案:

答案 0 :(得分:0)

在您的break;情况下,您只是缺少switch语句。