2015-07-21 17 views
6

Obecnie piszę aplikację przy użyciu SailsJS. To, co zostało zrobione do tej pory, działa zgodnie z oczekiwaniami, gdy testowane jest "ręcznie", ale nie sprawdza się w przypadku Mocha.Przeprowadzanie testów Mokka z SailsJS i Superagentem

Starałem się śledzić SailsJS testing guide, nazywając test z KMP:

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

Moje testy struktura katalogów jest następująca:

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); 
    }); 
    }); 
}); 

Wreszcie, tutaj jest moje wyjście gdy zgłoszę Test npm:

> [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. 

Podsumowując rzeczy, jestem testowania auth/UNAUTH API. Poniżej są zasady:

  • Gdy zalogowany użytkownik próbuje zalogować się w, „Nieuwierzytelniona” polityka powinna wyrzucić błąd (401)
  • Gdy zalogowany użytkownik spróbuje zalogować się w górę, „Nieuwierzytelniona "Polityka powinna rzucać błąd (401)
  • Gdy wylogować użytkownik próbuje zalogować się obecnie,«uwierzytelnione»polityka powinna rzucać się błąd (401)

mogę robić coś złego, ale ja naprawdę mogę nie wiem, co to jest. Czy możesz pomóc rozwiązać ten problem?

Jeśli potrzebujesz więcej informacji, zapytaj. Możesz znaleźć kod (bez testów, ponieważ nie działają) on Github.

Dzięki za przeczytanie, życzę miłego dnia!

UPDATE

Dzięki elsaar, zmieniłem mojego kodu:

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); 
    }); 
    }); 
}); 

Odpowiedz

4

myślę, że sesja nie jest utrwalone, a więc użytkownik ty loggedin we wcześniejszym wniosku nie będzie być zalogowanym w późniejszym żądaniu. W taki właśnie sposób powinny być testy jednostkowe. Musisz więc upewnić się, że użytkownik jest w pożądanym terminie (zalogowany lub wylogowany) przed uruchomieniem testu.

EDIT - Musisz używać tego samego wystąpienia czynnika supertest się utrzymywać sesję - https://github.com/visionmedia/supertest/issues/46#issuecomment-58534736

Więc po prostu zrobić to na początku swoich badań, i użyć tego samego środka we wszystkich testach

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

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

Tego właśnie szukałem. Dzięki! – FWoelffel