如何让Mocha失败测试 [英] How to get Mocha to fail a test

查看:114
本文介绍了如何让Mocha失败测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下测试:

it.only('validation should fail', function(done) {
    var body = {
        title: "dffdasfsdfsdafddfsadsa",
        description: "Postman Description",
        beginDate: now.add(3, 'd').format(),
        endDate: now.add(4, 'd').format()
    }


    var rules = eventsValidation.eventCreationRules();
    var valMessages = eventsValidation.eventCreationMessages();

    indicative
        .validateAll(rules, body, valMessages)
        .then(function(data) {
            console.log("SHOULD NOT GET HERE");
            should.fail("should not get here");
            done();

        })
        .catch(function(error) {
            console.log("SHOULD GET HERE");
            console.log(error);
        });
    done();
});

测试执行路径是正确的。当我验证数据时,它会不应该在这里。测试真的是确保它没有。当我输入非验证数据时,代码会转到应该在这里。所以验证规则有效。

The test execution path is correct. When I have validating data, it goes to "SHOULD NOT GET HERE". The test is really to make sure it doesn't. And when I put in non validating data the code does go to "SHOULD GET HERE". So the validation rules work.

我要做的是确保测试失败,当我有错误的验证数据并验证时。然而,当我运行它,因为它有良好的数据,它验证,运行失败,但摩卡仍然标记为传递。如果执行不应该在这里,我希望它失败。

What I'm trying to do is make sure the test fails when when I have bad validation data and it validates. However when I run it as it is with good data it validates, runs the fail, but mocha still marks it as the passing. I want it to fail if the execution gets to "SHOULD NOT GET HERE".

我尝试过抛出新错误(失败);也没有运气。在这两种情况下,它实际上似乎也在.catch块中运行代码。

I've tried throw new Error("fail"); as well with no luck. In both cases it actually seems to run the code in the .catch block as well.

有什么建议吗?
我在类似问题中找到了解决方法。写这个问题是因为这些解决方案对我来说似乎不起作用。

Any suggestions? I found solutions for this in a similar question. This question is written because those solutions don't seem to be working for me.

推荐答案

使用 chai -as-promised ,使用本机Mocha承诺处理程序。

Use chai-as-promised, with native Mocha promise handlers.

var chai = require('chai').use(require('chai-as-promised'));
var should = chai.should(); // This will enable .should for promise assertions

您不再需要已完成,只需返回承诺。

You no longer need done, simply return the promise.

// Remove `done` from the line below
it.only('validation should fail', function(/* done */) {
    var body = {
        title: "dffdasfsdfsdafddfsadsa",
        description: "Postman Description",
        beginDate: now.add(3, 'd').format(),
        endDate: now.add(4, 'd').format()
    }

    var rules = eventsValidation.eventCreationRules();
    var valMessages = eventsValidation.eventCreationMessages();

    // Return the promise
    return indicative
        .validateAll(rules, body, valMessages)
        .should.be.rejected; // The test will pass only if the promise is rejected

    // Remove done, we no longer need it
    // done();
});

这篇关于如何让Mocha失败测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆