- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 Node 创建一个 API,但我很难理解如何正确地对该 API 进行单元测试。 API 本身使用 Express 和 Mongo(带有 Mongoose)。
到目前为止,我已经能够为 API 端点本身的端到端测试创建集成测试。我使用 supertest、mocha 和 chai 进行集成测试,并使用 dotenv 在运行时使用测试数据库。 npm 测试脚本在集成测试运行之前设置要测试的环境。它运行良好。
但我还想为 Controller 功能等各种组件创建单元测试。
我很想使用 Sinon 进行单元测试,但我很难知道接下来要采取什么步骤。
我将详细介绍 API 的通用版本,该 API 被重写为每个人最喜欢的 Todos。
该应用程序具有以下目录结构:
api
|- todo
| |- controller.js
| |- model.js
| |- routes.js
| |- serializer.js
|- test
| |- integration
| | |- todos.js
| |- unit
| | |- todos.js
|- index.js
|- package.json
package.json
{
"name": "todos",
"version": "1.0.0",
"description": "",
"main": "index.js",
"directories": {
"doc": "docs"
},
"scripts": {
"test": "mocha test/unit --recursive",
"test-int": "NODE_ENV=test mocha test/integration --recursive"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.15.0",
"express": "^4.13.4",
"jsonapi-serializer": "^3.1.0",
"mongoose": "^4.4.13"
},
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^2.4.5",
"sinon": "^1.17.4",
"sinon-as-promised": "^4.0.0",
"sinon-mongoose": "^1.2.1",
"supertest": "^1.2.0"
}
}
index.js
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
// Configs
// I really use 'dotenv' package to set config based on environment.
// removed and defaults put in place for brevity
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
// Database
mongoose.connect('mongodb://localhost/todosapi');
//Middleware
app.set('port', 3000);
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
// Routers
var todosRouter = require('./api/todos/routes');
app.use('/todos', todosRouter);
app.listen(app.get('port'), function() {
console.log('App now running on http://localhost:' + app.get('port'));
});
module.exports = app;
serializer.js
(这纯粹是从 Mongo 获取输出并将其序列化为 JsonAPI 格式。所以它对于这个示例来说有点多余,但我保留了它,因为它是我目前在 api 中使用的东西。)
'use strict';
var JSONAPISerializer = require('jsonapi-serializer').Serializer;
module.exports = new JSONAPISerializer('todos', {
attributes: ['title', '_user']
,
_user: {
ref: 'id',
attributes: ['username']
}
});
routes.js
var router = require('express').Router();
var controller = require('./controller');
router.route('/')
.get(controller.getAll)
.post(controller.create);
router.route('/:id')
.get(controller.getOne)
.put(controller.update)
.delete(controller.delete);
module.exports = router;
model.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var todoSchema = new Schema({
title: {
type: String
},
_user: {
type: Schema.Types.ObjectId,
ref: 'User'
}
});
module.exports = mongoose.model('Todo', todoSchema);
controller.js
var Todo = require('./model');
var TodoSerializer = require('./serializer');
module.exports = {
getAll: function(req, res, next) {
Todo.find({})
.populate('_user', '-password')
.then(function(data) {
var todoJson = TodoSerializer.serialize(data);
res.json(todoJson);
}, function(err) {
next(err);
});
},
getOne: function(req, res, next) {
// I use passport for handling User authentication so assume the user._id is set at this point
Todo.findOne({'_id': req.params.id, '_user': req.user._id})
.populate('_user', '-password')
.then(function(todo) {
if (!todo) {
next(new Error('No todo item found.'));
} else {
var todoJson = TodoSerializer.serialize(todo);
return res.json(todoJson);
}
}, function(err) {
next(err);
});
},
create: function(req, res, next) {
// ...
},
update: function(req, res, next) {
// ...
},
delete: function(req, res, next) {
// ...
}
};
测试/单元/todos.js
var mocha = require('mocha');
var sinon = require('sinon');
require('sinon-as-promised');
require('sinon-mongoose');
var expect = require('chai').expect;
var app = require('../../index');
var TodosModel = require('../../api/todos/model');
describe('Routes: Todos', function() {
it('getAllTodos', function (done) {
// What goes here?
});
it('getOneTodoForUser', function (done) {
// What goes here?
});
});
现在我不想自己测试路由(我在此处未详述的集成测试中进行测试)。
我目前的想法是,下一个最好的办法是实际对 controller.getAll 或 controller.getOne 函数进行单元测试。然后使用 Sinon stub 通过 Mongoose 模拟对 Mongo 的调用。
但尽管已经阅读了 sinon 文档,但我不知道下一步该做什么:/
问题
test/unit/todos.js
以在可靠的单元测试状态下获得以上内容:/最终目标是运行 mocha test/unit
并让它对该 API 部分的各个部分进行单元测试
最佳答案
您好,我已经为您创建了一些测试以了解如何使用模拟。
完整示例 github/nodejs_unit_tests_example
controller.test.js
const proxyquire = require('proxyquire')
const sinon = require('sinon')
const faker = require('faker')
const assert = require('chai').assert
describe('todo/controller', () => {
describe('controller', () => {
let mdl
let modelStub, serializerStub, populateMethodStub, fakeData
let fakeSerializedData, fakeError
let mongoResponse
before(() => {
fakeData = faker.helpers.createTransaction()
fakeError = faker.lorem.word()
populateMethodStub = {
populate: sinon.stub().callsFake(() => mongoResponse)
}
modelStub = {
find: sinon.stub().callsFake(() => {
return populateMethodStub
}),
findOne: sinon.stub().callsFake(() => {
return populateMethodStub
})
}
fakeSerializedData = faker.helpers.createTransaction()
serializerStub = {
serialize: sinon.stub().callsFake(() => {
return fakeSerializedData
})
}
mdl = proxyquire('../todo/controller.js',
{
'./model': modelStub,
'./serializer': serializerStub
}
)
})
beforeEach(() => {
modelStub.find.resetHistory()
modelStub.findOne.resetHistory()
populateMethodStub.populate.resetHistory()
serializerStub.serialize.resetHistory()
})
describe('getAll', () => {
it('should return serialized search result from mongodb', (done) => {
let resolveFn
let fakeCallback = new Promise((res, rej) => {
resolveFn = res
})
mongoResponse = Promise.resolve(fakeData)
let fakeRes = {
json: sinon.stub().callsFake(() => {
resolveFn()
})
}
mdl.getAll(null, fakeRes, null)
fakeCallback.then(() => {
sinon.assert.calledOnce(modelStub.find)
sinon.assert.calledWith(modelStub.find, {})
sinon.assert.calledOnce(populateMethodStub.populate)
sinon.assert.calledWith(populateMethodStub.populate, '_user', '-password')
sinon.assert.calledOnce(serializerStub.serialize)
sinon.assert.calledWith(serializerStub.serialize, fakeData)
sinon.assert.calledOnce(fakeRes.json)
sinon.assert.calledWith(fakeRes.json, fakeSerializedData)
done()
}).catch(done)
})
it('should call next callback if mongo db return exception', (done) => {
let fakeCallback = (err) => {
assert.equal(fakeError, err)
done()
}
mongoResponse = Promise.reject(fakeError)
let fakeRes = sinon.mock()
mdl.getAll(null, fakeRes, fakeCallback)
})
})
describe('getOne', () => {
it('should return serialized search result from mongodb', (done) => {
let resolveFn
let fakeCallback = new Promise((res, rej) => {
resolveFn = res
})
mongoResponse = Promise.resolve(fakeData)
let fakeRes = {
json: sinon.stub().callsFake(() => {
resolveFn()
})
}
let fakeReq = {
params: {
id: faker.random.number()
},
user: {
_id: faker.random.number()
}
}
let findParams = {
'_id': fakeReq.params.id,
'_user': fakeReq.user._id
}
mdl.getOne(fakeReq, fakeRes, null)
fakeCallback.then(() => {
sinon.assert.calledOnce(modelStub.findOne)
sinon.assert.calledWith(modelStub.findOne, findParams)
sinon.assert.calledOnce(populateMethodStub.populate)
sinon.assert.calledWith(populateMethodStub.populate, '_user', '-password')
sinon.assert.calledOnce(serializerStub.serialize)
sinon.assert.calledWith(serializerStub.serialize, fakeData)
sinon.assert.calledOnce(fakeRes.json)
sinon.assert.calledWith(fakeRes.json, fakeSerializedData)
done()
}).catch(done)
})
it('should call next callback if mongodb return exception', (done) => {
let fakeReq = {
params: {
id: faker.random.number()
},
user: {
_id: faker.random.number()
}
}
let fakeCallback = (err) => {
assert.equal(fakeError, err)
done()
}
mongoResponse = Promise.reject(fakeError)
let fakeRes = sinon.mock()
mdl.getOne(fakeReq, fakeRes, fakeCallback)
})
it('should call next callback with error if mongodb return empty result', (done) => {
let fakeReq = {
params: {
id: faker.random.number()
},
user: {
_id: faker.random.number()
}
}
let expectedError = new Error('No todo item found.')
let fakeCallback = (err) => {
assert.equal(expectedError.message, err.message)
done()
}
mongoResponse = Promise.resolve(null)
let fakeRes = sinon.mock()
mdl.getOne(fakeReq, fakeRes, fakeCallback)
})
})
})
})
模型.test.js
const proxyquire = require('proxyquire')
const sinon = require('sinon')
const faker = require('faker')
describe('todo/model', () => {
describe('todo schema', () => {
let mongooseStub, SchemaConstructorSpy
let ObjectIdFake, mongooseModelSpy, SchemaSpy
before(() => {
ObjectIdFake = faker.lorem.word()
SchemaConstructorSpy = sinon.spy()
SchemaSpy = sinon.spy()
class SchemaStub {
constructor(...args) {
SchemaConstructorSpy(...args)
return SchemaSpy
}
}
SchemaStub.Types = {
ObjectId: ObjectIdFake
}
mongooseModelSpy = sinon.spy()
mongooseStub = {
"Schema": SchemaStub,
"model": mongooseModelSpy
}
proxyquire('../todo/model.js',
{
'mongoose': mongooseStub
}
)
})
it('should return new Todo model by schema', () => {
let todoSchema = {
title: {
type: String
},
_user: {
type: ObjectIdFake,
ref: 'User'
}
}
sinon.assert.calledOnce(SchemaConstructorSpy)
sinon.assert.calledWith(SchemaConstructorSpy, todoSchema)
sinon.assert.calledOnce(mongooseModelSpy)
sinon.assert.calledWith(mongooseModelSpy, 'Todo', SchemaSpy)
})
})
})
routes.test.js
const proxyquire = require('proxyquire')
const sinon = require('sinon')
const faker = require('faker')
describe('todo/routes', () => {
describe('router', () => {
let expressStub, controllerStub, RouterStub, rootRouteStub, idRouterStub
before(() => {
rootRouteStub = {
"get": sinon.stub().callsFake(() => rootRouteStub),
"post": sinon.stub().callsFake(() => rootRouteStub)
}
idRouterStub = {
"get": sinon.stub().callsFake(() => idRouterStub),
"put": sinon.stub().callsFake(() => idRouterStub),
"delete": sinon.stub().callsFake(() => idRouterStub)
}
RouterStub = {
route: sinon.stub().callsFake((route) => {
if (route === '/:id') {
return idRouterStub
}
return rootRouteStub
})
}
expressStub = {
Router: sinon.stub().returns(RouterStub)
}
controllerStub = {
getAll: sinon.mock(),
create: sinon.mock(),
getOne: sinon.mock(),
update: sinon.mock(),
delete: sinon.mock()
}
proxyquire('../todo/routes.js',
{
'express': expressStub,
'./controller': controllerStub
}
)
})
it('should map root get router with getAll controller', () => {
sinon.assert.calledWith(RouterStub.route, '/')
sinon.assert.calledWith(rootRouteStub.get, controllerStub.getAll)
})
it('should map root post router with create controller', () => {
sinon.assert.calledWith(RouterStub.route, '/')
sinon.assert.calledWith(rootRouteStub.post, controllerStub.create)
})
it('should map /:id get router with getOne controller', () => {
sinon.assert.calledWith(RouterStub.route, '/:id')
sinon.assert.calledWith(idRouterStub.get, controllerStub.getOne)
})
it('should map /:id put router with update controller', () => {
sinon.assert.calledWith(RouterStub.route, '/:id')
sinon.assert.calledWith(idRouterStub.put, controllerStub.update)
})
it('should map /:id delete router with delete controller', () => {
sinon.assert.calledWith(RouterStub.route, '/:id')
sinon.assert.calledWith(idRouterStub.delete, controllerStub.delete)
})
})
})
serializer.test.js
const proxyquire = require('proxyquire')
const sinon = require('sinon')
describe('todo/serializer', () => {
describe('json serializer', () => {
let JSONAPISerializerStub, SerializerConstructorSpy
before(() => {
SerializerConstructorSpy = sinon.spy()
class SerializerStub {
constructor(...args) {
SerializerConstructorSpy(...args)
}
}
JSONAPISerializerStub = {
Serializer: SerializerStub
}
proxyquire('../todo/serializer.js',
{
'jsonapi-serializer': JSONAPISerializerStub
}
)
})
it('should return new instance of Serializer', () => {
let schema = {
attributes: ['title', '_user']
,
_user: {
ref: 'id',
attributes: ['username']
}
}
sinon.assert.calledOnce(SerializerConstructorSpy)
sinon.assert.calledWith(SerializerConstructorSpy, 'todos', schema)
})
})
})
关于node.js - 如何使用 Sinon 对 Node API 进行单元测试(Express with Mongodb),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38190712/
我正在学习构建单页应用程序 (SPA) 所需的所有技术。总而言之,我想将我的应用程序实现为单独的层,其中前端仅使用 API Web 服务(json 通过 socket.io)与后端通信。前端基本上是
当我看到存储在我的数据库中的日期时。 这是 正常 。日期和时间就是这样。 但是当我运行 get 请求来获取数据时。 此格式与存储在数据库 中的格式不同。为什么会发生这种情况? 最佳答案 我认为您可以将
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在尝试使用backbone.js 实现一些代码 和 hogan.js (http://twitter.github.com/hogan.js/) Hogan.js was developed ag
我正在使用 Backbone.js、Node.js 和 Express.js 制作一个 Web 应用程序,并且想要添加用户功能(登录、注销、配置文件、显示内容与该用户相关)。我打算使用 Passpor
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
我尝试在 NodeJS 中加载数据,然后将其传递给 ExpressJS 以在浏览器中呈现 d3 图表。 我知道我可以通过这种方式加载数据 - https://github.com/mbostock/q
在 node.js 中,我似乎遇到了相同的 3 个文件名来描述应用程序的主要入口点: 使用 express-generator 包时,会创建一个 app.js 文件作为生成应用的主要入口点。 通过 n
最近,我有机会观看了 john papa 关于构建单页应用程序的精彩类(class)。我会喜欢的。它涉及服务器端和客户端应用程序的方方面面。 我更喜欢客户端。在他的实现过程中,papa先生在客户端有类
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我是一个图形新手,需要帮助了解各种 javascript 2D 库的功能。 . . 我从 Pixi.js 中得到了什么,而我没有从 Konva 等基于 Canvas 的库中得到什么? 我从 Konva
我正在尝试将一些 LESS 代码(通过 ember-cli-less)构建到 CSS 文件中。 1) https://almsaeedstudio.com/ AdminLTE LESS 文件2) Bo
尝试查看 Express Passport 中所有登录用户的所有 session ,并希望能够查看当前登录的用户。最好和最快的方法是什么? 我在想也许我可以在登录时执行此操作并将用户模型数据库“在线”
我有一个 React 应用程序,但我需要在组件加载完成后运行一些客户端 js。一旦渲染函数完成并加载,运行与 DOM 交互的 js 的最佳方式是什么,例如 $('div').mixItUp() 。对
请告诉我如何使用bodyparser.raw()将文件上传到express.js服务器 客户端 // ... onFilePicked(file) { const url = 'upload/a
我正在尝试从 Grunt 迁移到 Gulp。这个项目在 Grunt 下运行得很好,所以我一定是在 Gulp 中做错了什么。 除脚本外,所有其他任务均有效。我现在厌倦了添加和注释部分。 我不断收到与意外
我正在尝试更改我的网站名称。找不到可以设置标题或应用程序名称的位置。 最佳答案 您可以在 config/ 目录中创建任何文件,例如 config/app.js 包含如下内容: module.expor
经过多年的服务器端 PHP/MySQL 开发,我正在尝试探索用于构建现代 Web 应用程序的新技术。 我正在尝试对所有 JavaScript 内容进行排序,如果我理解得很好,一个有效的解决方案可以是服
我是 Nodejs 的新手。我在 route 目录中有一个 app.js 和一个 index.js。我有一个 app.use(multer....)。我还定义了 app.post('filter-re
我正在使用 angular-seed用于构建我的应用程序的模板。最初,我将所有 JavaScript 代码放入一个文件 main.js。该文件包含我的模块声明、 Controller 、指令、过滤器和
我是一名优秀的程序员,十分优秀!