我很擅长测试并试图了解如何测试一个相当简单的控制器操作。我遇到的问题是不知道如何完成模拟控制器内部的方法,甚至不知道何时适合这样做。
该操作会在我的webapp的db中创建一个挂起的用户,并返回一个加入链接。它看起来像这样:
module.exports = {
create: function(req, res, next) {
var name,
invitee = req.allParams();
if ( _.isEmpty(invitee) || invitee.name === undefined) {
return res.badRequest('Invalid parameters provided when trying to create a new pending user.');
}
// Parse the name into its parts.
name = invitee.name.split(' ');
delete invitee.name;
invitee.firstName = name[0];
invitee.lastName = name[1];
// Create the pending user and send response.
PendingUser.create(invitee, function(err, pending) {
var link;
if (err && err.code === 'E_VALIDATION') {
req.session.flash = { 'err': err };
return res.status(400).send({'err':err});
}
// Generate token & link
link = 'http://' + req.headers.host + '/join/' + pending.id;
// Respond with link.
res.json({'joinLink': link});
});
}
}
我为此方法编写的测试如下所示:
'use strict';
/**
* Tests for PendingUserController
*/
var PendingUserController = require('../../api/controllers/PendingUserController.js'),
sinon = require('sinon'),
assert = require('assert');
describe('Pending User Tests', function(done) {
describe('Call the create action with empty user data', function() {
it('should return 400', function(done) {
// Mock the req object.
var xhr = sinon.useFakeXMLHttpRequest();
xhr.allParams = function() {
return this.params;
};
xhr.badRequest
xhr.params = generatePendingUser(false, false, false, false);
var cb = sinon.spy();
PendingUserController.create(xhr, {
'cb': cb
});
assert.ok(cb.called);
});
});
}
function generatePendingUser(hasName, hasEmail, hasAffiliation, hasTitle) {
var pendingUser = {};
if (hasName) pendingUser.name = 'Bobbie Brown';
if (hasEmail) pendingUser.emailAddress = 'bobbie.brown@example.edu';
if (hasAffiliation) pendingUser.affiliation = 'Very Exclusive University';
if (hasTitle) pendingUser.title = "Assistant Professor";
return pendingUser;
}
由于我遇到的障碍,我的测试仍然不完整。从测试中可以看出,我试图模拟请求对象以及控制器操作req.allParams()
中调用的第一个方法。但是在控制器中可能调用的第二种方法是res.badRequest()
,它是function built into the res
object within sails.js。
这个功能我不知道如何模拟。此外,考虑嘲笑这个功能会引发各种其他问题。为什么我首先要嘲笑这个功能?我认为,单元测试的逻辑是,您可以与其他人一起单独测试部分代码,但这有点远吗?它还会产生大量额外的工作,因为我需要模拟这个函数的行为,这可能是也可能不容易实现。
我在此处撰写的代码基于一些概念验证类型教程(请参阅here,here和here),但这些帖子不处理控制器中req和/或res对象上有方法的问题。
在这里寻求解决方案的正确方法是什么?任何见解将不胜感激!
答案 0 :(得分:2)
您正尝试在挂起的用户控制器上测试创建操作并声明其响应/行为。您可以做的是实际使用Supertest发出请求进行测试。
我假设您已经使用Mocha& bootstrapped your test should.js。
var request = require('supertest');
describe('PendingUsersController', function() {
describe('#create()', function() {
it('should create a pending user', function (done) {
request(sails.hooks.http.app)
.post('/pendinguser')
//User Data
.send({ name: 'test', emailAdress: 'test@test.mail', affiliation: 'University of JavaScript', title: 'Software Engineer' })
.expect(200)
.end(function (err, res) {
//true if response contains { message : "Your are pending user."}
res.body.message.should.be.eql("Your are pending user.");
});
});
});
});
有关控制器测试的更多信息Sails.js from docs或查看this example项目了解更多信息。