nodejs-tw / ama

Ask me anything!
MIT License
31 stars 1 forks source link

測試Promise.catch #4

Closed RammusXu closed 8 years ago

RammusXu commented 8 years ago

目的

測試Promise.catch的值是否正確

操作流程

mocha test.js

遇到的問題

在catch裡面放assert會造成mocha 無法執行到done() 所以會timeout

嘗試過的解法

各種google只找到怎麼測試Promise.resolve,但是都沒有提到catch裡面放assert

程式碼

下面附上我的程式碼以及我遇到的錯誤

程式碼

describe('CompanyProfile', function() {
    it('duplate username will not save anything', function(done) {

        profileUtil.createUser('test', 'qwer1234', 'test', 'Company')
            .then(function(value, some) {
                console.log("value", value);
                // should.not.exist(value);
                return profileUtil.createUser('test', 'qwer1234', 'test2', 'Company');
            })
            .then(function(value) {
                console.log("in2");
            })
            .catch(function(err) {
                console.log("err", err);
                should.not.exist(err);
                // should.not.exist(err);
                done();
            });
});

錯誤

value { user: 
   { menu_crud: [],
     _id: 57301f3488fda54746bfbdde,
     email: 'test',
     pwd: 'qwer1234',
     custom: { _profile: 57301f3488fda54746bfbddd },
     __v: 0 },
  profile: 
   { who_u_r: [],
     what_u_do: [],
     size: '1-10',
     location: {},
     culture: [],
     technology: [],
     links: {},
     _id: 57301f3488fda54746bfbddd,
     footer_pic: 'img/profile_img/cover-photo.jpg',
     cover_pic: 'img/profile_img/cover-photo.jpg',
     pic: 'img/alpaca2.jpg',
     time: Mon May 09 2016 13:25:08 GMT+0800 (CST),
     type: 'Company',
     username: 'test',
     __v: 0 } }
err { [MongoError: E11000 duplicate key error collection: test.users index: system_parameter_1_email_1 dup key: { : null, : "test" }]
  name: 'MongoError',
  message: 'E11000 duplicate key error collection: test.users index: system_parameter_1_email_1 dup key: { : null, : "test" }',
  driver: true,
  code: 11000,
  index: 0,
  errmsg: 'E11000 duplicate key error collection: test.users index: system_parameter_1_email_1 dup key: { : null, : "test" }',
  getOperation: [Function],
  toJSON: [Function],
  toString: [Function] }
    1) duplate username will not save anything
    - shouldn't save same profile.username

  1) CompanyProfile duplate username will not save anything:
     Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
dca commented 8 years ago
            .catch(function(err) {
                console.log("err", err);
                should.not.exist(err);
                // should.not.exist(err);
                done();
            });

上面這裡把這一行拿掉試試看? should.not.exist(err);

因為會進來這裡一定有 err,或者可以改成 should.exist(err); 試試看

RammusXu commented 8 years ago

@dca 我是希望console能夠顯示錯誤是什麼,所以才故意放should.not.exist(err);

應該是要顯示

 CompanyProfile
    - try it
    1) duplate username will not save anything
    - shouldn't save same profile.username

  0 passing (65ms)
  2 pending
  1 failing

  1) CompanyProfile duplate username will not save anything:
     AssertionError: expected 'err' to not exist
      at doAsserterAsyncAndAddThen (/Users/rammus/git_workspace/Ping/app/node_modules/chai-as-promised/lib/chai-as-promised.js:296:33)
      at null.<anonymous> (/Users/rammus/git_workspace/Ping/app/node_modules/chai-as-promised/lib/chai-as-promised.js:286:25)
      at Object.defineProperty.get (/Users/rammus/git_workspace/Ping/app/node_modules/chai/lib/chai/utils/overwriteProperty.js:50:37)
      at Object.should.not.exist (/Users/rammus/git_workspace/Ping/app/node_modules/chai/lib/chai/interface/should.js:190:37)
      at Context.<anonymous> (profile_test.js:77:20)
      at clearDB (profile_test.js:31:16)
      at profile_test.js:37:20
      at /Users/rammus/git_workspace/Ping/app/node_modules/mongoose/lib/connection.js:284:19
      at open (/Users/rammus/git_workspace/Ping/app/node_modules/mongoose/lib/connection.js:511:17)
      at NativeConnection.Connection.onOpen (/Users/rammus/git_workspace/Ping/app/node_modules/mongoose/lib/connection.js:521:5)
      at /Users/rammus/git_workspace/Ping/app/node_modules/mongoose/lib/connection.js:477:11
      at /Users/rammus/git_workspace/Ping/app/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:60:5
      at /Users/rammus/git_workspace/Ping/app/node_modules/mongoose/node_modules/mongodb/lib/db.js:234:5
      at connectHandler (/Users/rammus/git_workspace/Ping/app/node_modules/mongoose/node_modules/mongodb/lib/server.js:306:7)
      at /Users/rammus/git_workspace/Ping/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:540:23
      at commandCallback (/Users/rammus/git_workspace/Ping/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:1161:9)
      at Callbacks.emit (/Users/rammus/git_workspace/Ping/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:119:3)
      at null.messageHandler (/Users/rammus/git_workspace/Ping/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:295:23)
      at Socket.<anonymous> (/Users/rammus/git_workspace/Ping/app/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:285:22)
      at readableAddChunk (_stream_readable.js:146:16)
      at Socket.Readable.push (_stream_readable.js:110:10)
      at TCP.onread (net.js:529:20)

而不是Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

RammusXu commented 8 years ago

感謝FB:Chiahao Lin 大大的解法 拿掉callback: done 直接return promise就可以了 REF: http://tobyho.com/2015/12/16/mocha-with-promises/

describe('CompanyProfile', function() {
    it('duplate username will not save anything', function() {

        return profileUtil.createUser('test', 'qwer1234', 'test', 'Company')
            .then(function(value, some) {
                console.log("value", value);
                // should.not.exist(value);
                return profileUtil.createUser('test', 'qwer1234', 'test2', 'Company');
            })
            .then(function(value) {
                console.log("in2");
            })
            .catch(function(err) {
                console.log("err", err);
                should.not.exist(err);
                // should.not.exist(err);
            });
});
purepennons commented 8 years ago

硬要用 done() 的話,可能是 error 觸發的時間比 mocha 設定 timeout 的時間還久。 所以可以拉長 timeout 時間看看。

describe('CompanyProfile', function() {
   this.timeout(20000)  // 拉長點,這個地方有個限制就是 describe 的 callback 不可以用 es6 的 arrow function 撰寫
    it('duplate username will not save anything', function() {

        return profileUtil.createUser('test', 'qwer1234', 'test', 'Company')
            .then(function(value, some) {
                console.log("value", value);
                // should.not.exist(value);
                return profileUtil.createUser('test', 'qwer1234', 'test2', 'Company');
            })
            .then(function(value) {
                console.log("in2");
            })
            .catch(function(err) {
                console.log("err", err);
                should.not.exist(err);
                // should.not.exist(err);
                done();
            });
});
RammusXu commented 8 years ago

感謝 @purepennons @dca 大大的幫忙 最後做出來的成果是


    it('duplate username will not save anything', function() {

        return profileUtil.createUser('test', 'qwer1234', 'test', 'Company')
            .then(function(value, some) {
                should.exist(value);
                return profileUtil.createUser('test', 'qwer1234', 'test2', 'Company');
            })
            .then(function(value) {
                should.not.exist(value);
            }, function(err) {
                (err.message).should.include('E11000 duplicate key error collection: test.users index: system_parameter_1_email_1 dup');
            })
            .catch(function(err) {
                console.log("err", err);
                should.not.exist(err);
            });

    });