2015-07-21 91 views
6

我目前正在使用SailsJS編寫應用程序。到目前爲止所做的工作都是按照預期進行的,而不是在用摩卡進行測試時。使用SailsJS和Superagent運行摩卡測試

我試圖按照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: '[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); 
    }); 
    }); 
}); 

最後,這裏是我的輸出,當我打電話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. 

總結的東西了,我測試一個auth/UNAUTH API。下面一個是政策:

  • 當登錄的用戶試圖登錄時,「未經驗證的」政策應該拋出一個錯誤(401)
  • 當登錄的用戶嘗試註冊時,「未認證「政策應該拋出一個錯誤(401)
  • 當註銷用戶嘗試登錄時,將‘證實’的政策應該拋出一個錯誤(401)

我可能做錯了什麼,但我真的不知道它是什麼。你能幫助解決這個問題嗎?

如果您需要更多信息,請詢問。你可能會發現代碼(沒有測試,因爲它們失敗)on Github

感謝您的閱讀,祝您有個愉快的一天!

UPDATE

感謝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: '[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); 
    }); 
    }); 
}); 

回答

4

我認爲會議沒有被堅持着,所以用戶已在前面的請求的loggedIn不會在稍後的請求中進行登錄。單元測試應該是這樣的。因此,在測試運行之前,您必須確保用戶處於期望的狀態(登錄或退出)。

編輯 - 你需要使用supertest代理的同一實例,以保存會話 - 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 
+0

這就是我一直在尋找的東西。謝謝! – FWoelffel