运行 使用 SailsJS 和 Superagent 进行 Mocha 测试
Running Mocha tests with SailsJS and Superagent
我目前正在使用 SailsJS 编写应用程序。到目前为止所做的工作在测试时按预期工作 'manually',但在使用 Mocha 测试时却没有。
我试着按照 SailsJS testing guide,用 npm 调用测试:
[...]
"scripts": {
"start": "node app.js",
"debug": "node debug app.js",
"test": "mocha test/bootstrap.test.js test/unit/**/*.test.js"
},
[...]
我的测试目录结构如下:
test
├── bootstrap.test.js
├── mocha.opts
└── unit
└── controllers
└── UserController.test.js
boostrap.test.js:
var Sails = require('sails');
var sails;
before(function(done) {
Sails.lift(function(err, server) {
sails = server;
if (err) return done(err);
done(err, sails);
});
});
after(function(done) {
Sails.lower(done);
});
UserController.test.js:
var request = require('supertest');
describe('UsersController', function() {
describe('#logout()', function() {
it('should respond with a 401 status because nobody is logged in', function (done) {
request(sails.hooks.http.app)
.put('/user/logout')
.expect(401, done)
});
});
describe('#signup()', function() {
it('should create and log in an user', function (done) {
request(sails.hooks.http.app)
.post('/user')
.send({
firstname: 'foo',
name: 'bar',
email: 'foo@bar.com',
sex: true,
password: 'foobar',
birthdate: '01/01/1991',
phoneNumber: '+33 3 10 10 10'
})
.expect(200, done)
});
});
describe('#logout()', function() {
it('should log out an user', function (done) {
request(sails.hooks.http.app)
.put('/user/logout')
.expect(200, done)
});
});
describe('#login()', function() {
it('should respond with a 404 status because credentials are invalid', function (done) {
request(sails.hooks.http.app)
.put('/user/login')
.send({
email: 'bar@foo.com',
password: 'barfoo'
})
.expect(404, done)
});
});
describe('#login()', function() {
it('should log in an user', function (done) {
request(sails.hooks.http.app)
.put('/user/login')
.send({
email: 'foo@bar.com',
password: 'foobar'
})
.expect(200, done);
});
});
describe('#login()', function() {
it('should respond with a 401 status because user is already logged in', function (done) {
request(sails.hooks.http.app)
.put('/user/login')
.send({
email: 'foo@bar.com',
password: 'foobar'
})
.expect(401, done);
});
});
});
最后,这是我调用 npm test:
时的输出
> sails@0.0.0 test /Users/fwoelffel/Dev/STOFMA
> mocha test/bootstrap.test.js test/unit/**/*.test.js
UsersController
#logout()
debug: false
debug: req.session -> {"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"}}
info: Policy 'authenticated' disallowed to proceed to the next policy
✓ should respond with a 401 status because nobody is logged in (86ms)
#signup()
debug: null === req.session.authenticated || undefined === req.session.authenticated -> true
debug: req.session -> {"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"}}
info: Policy 'unauthenticated' allowed to proceed to the next policy
info: User foo@bar.com signed up and logged in.
✓ should create and log in an user (146ms)
#logout()
debug: false
debug: req.session -> {"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"}}
info: Policy 'authenticated' disallowed to proceed to the next policy
1) should log out an user
#login()
debug: null === req.session.authenticated || undefined === req.session.authenticated -> true
debug: req.session -> {"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"}}
info: Policy 'unauthenticated' allowed to proceed to the next policy
info: No user matching bar@foo.com.
✓ should respond with a 404 status because credentials are invalid (66ms)
#login()
debug: null === req.session.authenticated || undefined === req.session.authenticated -> true
debug: req.session -> {"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"}}
info: Policy 'unauthenticated' allowed to proceed to the next policy
info: Found user foo@bar.com.
info: foo@bar.com credentials are valid.
✓ should log in an user (128ms)
#login()
debug: null === req.session.authenticated || undefined === req.session.authenticated -> true
debug: req.session -> {"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"}}
info: Policy 'unauthenticated' allowed to proceed to the next policy
info: Found user foo@bar.com.
info: foo@bar.com credentials are valid.
2) should respond with a 401 status because user is already logged in
4 passing (1s)
2 failing
1) UsersController #logout() should log out an user:
Error: expected 200 "OK", got 401 "Unauthorized"
at net.js:1419:10
2) UsersController #login() should respond with a 401 status because user is already logged in:
Error: expected 401 "Unauthorized", got 200 "OK"
at net.js:1419:10
npm ERR! Test failed. See above for more details.
总而言之,我正在测试 auth/unauth API。以下是政策:
- 当登录用户尝试登录时,'unauthenticated' 策略应抛出错误 (401)
- 当登录用户尝试注册时,'unauthenticated' 策略应抛出错误 (401)
- 当已注销的用户尝试注销时,'authenticated' 策略应抛出错误 (401)
我可能做错了什么,但我真的不知道是什么。你能帮忙解决这个问题吗?
如果您需要更多信息,请询问。您可能会找到代码(没有测试,因为它们失败了)on Github.
感谢阅读,祝您愉快!
更新
感谢 elsaar,我将代码更改为:
var request = require('supertest');
var agent;
describe('UsersController', function() {
before(function(done) {
agent = request.agent(sails.hooks.http.app);
done();
})
describe('#logout()', function() {
it('should respond with a 401 status because nobody is logged in', function (done) {
agent
.put('/user/logout')
.expect(401, done)
});
});
describe('#signup()', function() {
it('should create and log in an user', function (done) {
agent
.post('/user')
.send({
firstname: 'foo',
name: 'bar',
email: 'foo@bar.com',
sex: true,
password: 'foobar',
birthdate: '01/01/1991',
phoneNumber: '+33 3 10 10 10'
})
.expect(200, done)
});
});
describe('#logout()', function() {
it('should log out an user', function (done) {
agent
.put('/user/logout')
.expect(200, done)
});
});
describe('#login()', function() {
it('should respond with a 404 status because credentials are invalid', function (done) {
agent
.put('/user/login')
.send({
email: 'bar@foo.com',
password: 'barfoo'
})
.expect(404, done)
});
});
describe('#login()', function() {
it('should log in an user', function (done) {
agent
.put('/user/login')
.send({
email: 'foo@bar.com',
password: 'foobar'
})
.expect(200, done);
});
});
describe('#login()', function() {
it('should respond with a 401 status because user is already logged in', function (done) {
agent
.put('/user/login')
.send({
email: 'foo@bar.com',
password: 'foobar'
})
.expect(401, done);
});
});
});
我认为会话没有被持久化,所以您在之前的请求中登录的用户不会在以后的请求中登录。单元测试应该是这样的。因此,在测试 运行.
之前,您必须确保用户处于所需的状态(登录或注销)
编辑 - 您需要使用相同的超级测试代理实例来保持您的会话 - https://github.com/visionmedia/supertest/issues/46#issuecomment-58534736
所以只需在测试开始时执行此操作,并在所有测试中使用相同的代理
var supertest = require('supertest');
agent = supertest.agent(sails.hooks.http.app);
// the use the agent to test your endpoints
我目前正在使用 SailsJS 编写应用程序。到目前为止所做的工作在测试时按预期工作 'manually',但在使用 Mocha 测试时却没有。
我试着按照 SailsJS testing guide,用 npm 调用测试:
[...]
"scripts": {
"start": "node app.js",
"debug": "node debug app.js",
"test": "mocha test/bootstrap.test.js test/unit/**/*.test.js"
},
[...]
我的测试目录结构如下:
test
├── bootstrap.test.js
├── mocha.opts
└── unit
└── controllers
└── UserController.test.js
boostrap.test.js:
var Sails = require('sails');
var sails;
before(function(done) {
Sails.lift(function(err, server) {
sails = server;
if (err) return done(err);
done(err, sails);
});
});
after(function(done) {
Sails.lower(done);
});
UserController.test.js:
var request = require('supertest');
describe('UsersController', function() {
describe('#logout()', function() {
it('should respond with a 401 status because nobody is logged in', function (done) {
request(sails.hooks.http.app)
.put('/user/logout')
.expect(401, done)
});
});
describe('#signup()', function() {
it('should create and log in an user', function (done) {
request(sails.hooks.http.app)
.post('/user')
.send({
firstname: 'foo',
name: 'bar',
email: 'foo@bar.com',
sex: true,
password: 'foobar',
birthdate: '01/01/1991',
phoneNumber: '+33 3 10 10 10'
})
.expect(200, done)
});
});
describe('#logout()', function() {
it('should log out an user', function (done) {
request(sails.hooks.http.app)
.put('/user/logout')
.expect(200, done)
});
});
describe('#login()', function() {
it('should respond with a 404 status because credentials are invalid', function (done) {
request(sails.hooks.http.app)
.put('/user/login')
.send({
email: 'bar@foo.com',
password: 'barfoo'
})
.expect(404, done)
});
});
describe('#login()', function() {
it('should log in an user', function (done) {
request(sails.hooks.http.app)
.put('/user/login')
.send({
email: 'foo@bar.com',
password: 'foobar'
})
.expect(200, done);
});
});
describe('#login()', function() {
it('should respond with a 401 status because user is already logged in', function (done) {
request(sails.hooks.http.app)
.put('/user/login')
.send({
email: 'foo@bar.com',
password: 'foobar'
})
.expect(401, done);
});
});
});
最后,这是我调用 npm test:
时的输出> sails@0.0.0 test /Users/fwoelffel/Dev/STOFMA
> mocha test/bootstrap.test.js test/unit/**/*.test.js
UsersController
#logout()
debug: false
debug: req.session -> {"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"}}
info: Policy 'authenticated' disallowed to proceed to the next policy
✓ should respond with a 401 status because nobody is logged in (86ms)
#signup()
debug: null === req.session.authenticated || undefined === req.session.authenticated -> true
debug: req.session -> {"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"}}
info: Policy 'unauthenticated' allowed to proceed to the next policy
info: User foo@bar.com signed up and logged in.
✓ should create and log in an user (146ms)
#logout()
debug: false
debug: req.session -> {"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"}}
info: Policy 'authenticated' disallowed to proceed to the next policy
1) should log out an user
#login()
debug: null === req.session.authenticated || undefined === req.session.authenticated -> true
debug: req.session -> {"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"}}
info: Policy 'unauthenticated' allowed to proceed to the next policy
info: No user matching bar@foo.com.
✓ should respond with a 404 status because credentials are invalid (66ms)
#login()
debug: null === req.session.authenticated || undefined === req.session.authenticated -> true
debug: req.session -> {"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"}}
info: Policy 'unauthenticated' allowed to proceed to the next policy
info: Found user foo@bar.com.
info: foo@bar.com credentials are valid.
✓ should log in an user (128ms)
#login()
debug: null === req.session.authenticated || undefined === req.session.authenticated -> true
debug: req.session -> {"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"}}
info: Policy 'unauthenticated' allowed to proceed to the next policy
info: Found user foo@bar.com.
info: foo@bar.com credentials are valid.
2) should respond with a 401 status because user is already logged in
4 passing (1s)
2 failing
1) UsersController #logout() should log out an user:
Error: expected 200 "OK", got 401 "Unauthorized"
at net.js:1419:10
2) UsersController #login() should respond with a 401 status because user is already logged in:
Error: expected 401 "Unauthorized", got 200 "OK"
at net.js:1419:10
npm ERR! Test failed. See above for more details.
总而言之,我正在测试 auth/unauth API。以下是政策:
- 当登录用户尝试登录时,'unauthenticated' 策略应抛出错误 (401)
- 当登录用户尝试注册时,'unauthenticated' 策略应抛出错误 (401)
- 当已注销的用户尝试注销时,'authenticated' 策略应抛出错误 (401)
我可能做错了什么,但我真的不知道是什么。你能帮忙解决这个问题吗?
如果您需要更多信息,请询问。您可能会找到代码(没有测试,因为它们失败了)on Github.
感谢阅读,祝您愉快!
更新
感谢 elsaar,我将代码更改为:
var request = require('supertest');
var agent;
describe('UsersController', function() {
before(function(done) {
agent = request.agent(sails.hooks.http.app);
done();
})
describe('#logout()', function() {
it('should respond with a 401 status because nobody is logged in', function (done) {
agent
.put('/user/logout')
.expect(401, done)
});
});
describe('#signup()', function() {
it('should create and log in an user', function (done) {
agent
.post('/user')
.send({
firstname: 'foo',
name: 'bar',
email: 'foo@bar.com',
sex: true,
password: 'foobar',
birthdate: '01/01/1991',
phoneNumber: '+33 3 10 10 10'
})
.expect(200, done)
});
});
describe('#logout()', function() {
it('should log out an user', function (done) {
agent
.put('/user/logout')
.expect(200, done)
});
});
describe('#login()', function() {
it('should respond with a 404 status because credentials are invalid', function (done) {
agent
.put('/user/login')
.send({
email: 'bar@foo.com',
password: 'barfoo'
})
.expect(404, done)
});
});
describe('#login()', function() {
it('should log in an user', function (done) {
agent
.put('/user/login')
.send({
email: 'foo@bar.com',
password: 'foobar'
})
.expect(200, done);
});
});
describe('#login()', function() {
it('should respond with a 401 status because user is already logged in', function (done) {
agent
.put('/user/login')
.send({
email: 'foo@bar.com',
password: 'foobar'
})
.expect(401, done);
});
});
});
我认为会话没有被持久化,所以您在之前的请求中登录的用户不会在以后的请求中登录。单元测试应该是这样的。因此,在测试 运行.
之前,您必须确保用户处于所需的状态(登录或注销)编辑 - 您需要使用相同的超级测试代理实例来保持您的会话 - https://github.com/visionmedia/supertest/issues/46#issuecomment-58534736
所以只需在测试开始时执行此操作,并在所有测试中使用相同的代理
var supertest = require('supertest');
agent = supertest.agent(sails.hooks.http.app);
// the use the agent to test your endpoints