2015-07-21 16 views
6

Tôi hiện đang viết một ứng dụng bằng SailsJS. Những gì đã được thực hiện cho đến nay hoạt động như mong đợi khi thử nghiệm 'thủ công', nhưng không phải khi thử nghiệm với Mocha.Chạy thử nghiệm Mocha với SailsJS và Superagent

Tôi cố gắng để làm theo SailsJS testing guide, gọi thử nghiệm với NPM:

[...] 
"scripts": { 
    "start": "node app.js", 
    "debug": "node debug app.js", 
    "test": "mocha test/bootstrap.test.js test/unit/**/*.test.js" 
}, 
[...] 

cấu trúc thư mục thử nghiệm của tôi như sau:

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: '[email protected]', 
      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: '[email protected]', 
      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: '[email protected]', 
      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: '[email protected]', 
      password: 'foobar' 
     }) 
     .expect(401, done); 
    }); 
    }); 
}); 

Cuối cùng, đây là đầu ra của tôi khi tôi gọi NPM kiểm tra:

> [email protected] 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 [email protected] 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 [email protected] 
     ✓ 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 [email protected] 
info: [email protected] 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 [email protected] 
info: [email protected] 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. 

Tổng hợp những thứ lên, tôi đang thử nghiệm một auth/unauth API. Một Dưới đây là những chính sách:

  • Khi đăng nhập người dùng cố gắng đăng nhập, chính sách 'không được thẩm định' nên ném ra một lỗi (401)
  • Khi đăng nhập người dùng cố gắng đăng ký, 'không được thẩm định 'chính sách nên ném ra một lỗi (401)
  • Khi người dùng đăng xuất khỏi cố gắng thoát ra, các chính sách thực' nên ném ra một lỗi (401)

tôi có thể làm điều gì đó sai nhưng tôi thực sự có thể 'Con số đó là gì. Bạn có thể giúp giải quyết vấn đề này không?

Nếu bạn cần thêm thông tin, vui lòng hỏi. Bạn có thể tìm thấy mã (không có kiểm tra, vì chúng không thành công) on Github.

Cảm ơn bạn đã đọc, chúc một ngày tốt lành!

CẬP NHẬT

Nhờ elsaar, tôi đã thay đổi mã của tôi để:

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: '[email protected]', 
      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: '[email protected]', 
      password: 'barfoo' 
     }) 
     .expect(404, done) 
    }); 
    }); 

    describe('#login()', function() { 
    it('should log in an user', function (done) { 
     agent 
     .put('/user/login') 
     .send({ 
      email: '[email protected]', 
      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: '[email protected]', 
      password: 'foobar' 
     }) 
     .expect(401, done); 
    }); 
    }); 
}); 

Trả lời

4

Tôi nghĩ rằng phiên không được tồn, do đó, một người dùng mà bạn LOGGEDIN trong một yêu cầu trước đó sẽ không được đăng nhập trong một yêu cầu sau. Đó là cách kiểm tra đơn vị nên được. Vì vậy, bạn sẽ phải đảm bảo rằng người dùng là trong mong muốn đã nêu (đăng nhập hoặc ra) trước khi chạy thử nghiệm.

EDIT - Bạn cần sử dụng cùng một ví dụ của các đại lý supertest để tồn tại phiên của bạn - https://github.com/visionmedia/supertest/issues/46#issuecomment-58534736

Vì vậy, chỉ làm điều này ở phần đầu của bài kiểm tra của bạn, và sử dụng các đại lý như nhau trong tất cả các test

var supertest = require('supertest'); 
    agent = supertest.agent(sails.hooks.http.app); 

// the use the agent to test your endpoints 
+0

Đó là những gì tôi đang tìm kiếm. Cảm ơn! – FWoelffel

Các vấn đề liên quan