Node.js mocha测试:AssertionError:期望true为false

时间:2016-04-12 11:43:56

标签: node.js cordova unit-testing mocha

我写了一个用于测试Email not found案例的摩卡脚本。这是代码:

    it('Returns "Email not found"', function(done) {

    userModelMock.seedUsers();
    var testUser = userModelMock.getTestUser(),
        nonExistentEmailAddress = 'dummy@email.com';

    controller.resetPassword(nonExistentEmailAddress, function(err, apiResponse) {

        if (err) return done(err);
        should(apiResponse.success).equal(false);
        should(apiResponse.extras.msg)
            .equal(ApiMessages.EMAIL_NOT_FOUND);
        done();
    })
});  

我在终端中运行mocha时遇到此错误:

 1)  Returns "Email not found":

     AssertionError: expected true to be false
     + expected - actual

     -true
     +false

     at Assertion.fail (D:\PhoneGap_Projects\login\www\server\node_modules\should\lib\assertion.js:92:17)
     at Assertion.Object.defineProperty.value (D:\PhoneGap_Projects\login\www\server\node_modules\should\lib\assertion.js:164:1
     at D:\PhoneGap_Projects\login\www\server\test\account-controller-test.js:211:37
     at D:\PhoneGap_Projects\login\www\server\controllers\account.js:186:16
     at UserMock.findOne (D:\PhoneGap_Projects\login\www\server\test\user-mock.js:65:12)
     at AccountController.resetPassword (D:\PhoneGap_Projects\login\www\server\controllers\account.js:166:18)
     at Context.<anonymous> (D:\PhoneGap_Projects\login\www\server\test\account-controller-test.js:208:16)
     at callFnAsync (C:\Users\Ajay Kulkarni-enEXL\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:338:8)
     at Test.Runnable.run (C:\Users\Ajay Kulkarni-enEXL\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:290:7)
     at Runner.runTest (C:\Users\Ajay Kulkarni-enEXL\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:422:10)
     at C:\Users\Ajay Kulkarni-enEXL\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:533:12
     at next (C:\Users\Ajay Kulkarni-enEXL\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:342:14)
     at C:\Users\Ajay Kulkarni-enEXL\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:352:7
     at next (C:\Users\Ajay Kulkarni-enEXL\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:284:14)
     at C:\Users\Ajay Kulkarni-enEXL\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:315:7
     at done (C:\Users\Ajay Kulkarni-enEXL\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:276:5)
     at C:\Users\Ajay Kulkarni-enEXL\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:349:7
     at Context.<anonymous> (D:\PhoneGap_Projects\login\www\server\test\account-controller-test.js:25:5)
     at callFnAsync (C:\Users\Ajay Kulkarni-enEXL\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:338:8)
     at Hook.Runnable.run (C:\Users\Ajay Kulkarni-enEXL\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:290:7)
     at next (C:\Users\Ajay Kulkarni-enEXL\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:298:10)
     at Immediate._onImmediate (C:\Users\Ajay Kulkarni-enEXL\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:320:5)  

该错误发生在以下几行:

if (err) return done(err);
        should(apiResponse.success).equal(false);
        should(apiResponse.extras.msg)
            .equal(ApiMessages.EMAIL_NOT_FOUND);
        done();  

如何修复该错误?

1 个答案:

答案 0 :(得分:1)

嗯,这不是错误。只是您在测试中写道,您希望apiResponse.successfalse。但是你的api发回true,所以测试失败了。因此,您的测试运行正常,但您的api应该success发送到true,或者您的测试应该successtrue