node.js - sinon mocking GET, POST, PUT and DELETE methods assertions fail everytime. no idea why
问题描述
am learning sinon and nodeUnit for nodejs. wrote simple module that handles GET, POST, PUT and DELETE methods.
then started writing unit test. Now there are two sides for my problem:
One being: in GET
test when i specify requestMock.method = 'GET';
then
run nodeunit test-http-module.js
test fails, when reviewing call stack it says POST request failed (handlePostRequest as highlighted in call stack below).
Two being: when changing requestMock.method = 'GET';
to 'POST', 'PUT' or 'DELETE'
test still fails no matter what i change! but in latter cases the call stack shows the correct method being directed to.
I think there is a single problem causing the continuous failures and that GET
being directed to POST
.
please explain what is wrong.
ExpectationError: Unexpected call: writeHead(200, { Content-Type: "text/plain" })
Expectation met: writeHead(200, { Content-Type: "text/plain" }[, ...]) once
at Object.fail (D:\Projects\node-book\node_modules\sinon\lib\sinon\mock-expectation.js:281:25)
at Object.invokeMethod (D:\Projects\node-book\node_modules\sinon\lib\sinon\mock.js:174:25)
at Object.writeHead (D:\Projects\node-book\node_modules\sinon\lib\sinon\mock.js:67:35)
at
handlePostRequest
(D:\Projects\node-book\modules\http-module.js:8:14)at Object.handleRequest (D:\Projects\node-book\modules\http-module.js:34:26)
at Object.exports.handleGetRequestTest (D:\Projects\node-book\unit-testing-intro\test-http-module.js:22:21)
解决方案
You are just missing break;
statements in the cases of your switch
.
推荐阅读
- ibm-cloud - IBM Watson Studio 或 Watson Machine Learning 中的 WML_INSTANCE_NAME 是什么
- python - 如何指定要从 Yahoo Finance API 返回而不是获取所有字段?
- scala - 将 spark 数据框中的值限制为仅指定的值
- python - 如何添加到 websocket on_message 中的 manager.dict()
- firebase - GCP MFA 是否允许受信任的设备?
- python - 平均使用 *args 和类的属性
- azure-data-lake - 如何将 Azure Data Lake Gen1 和 Gen2 附加到单个 Spark HD Insight 群集?
- java - Java 枚举未与发布请求 Micronaut 2.3.4 中的多部分表单数据映射
- python - pyOpenGL GLUT 窗口功能无法正确关闭
- git - Jenkins 连接到 bitBucket 但拒绝克隆