QUnit, assert not OK?

2020-04-10 01:34发布

Sorry if this is obvious, but is there a notOK or equivalent function in QUnit, if we want to assert that a method returns false?

I can't see a way to negate OK in the documentation.

I tried:

!ok...

but that didn't work.

标签: qunit
5条回答
Rolldiameter
2楼-- · 2020-04-10 02:08

starting from qunit 1.18 there is a dedicated function:

assert.notOk(valueToBeTested);
查看更多
ら.Afraid
3楼-- · 2020-04-10 02:13

The better approach would be to use:

notOk(<something>);

as it will be more expressive than stating:

ok(!<something>);
查看更多
我命由我不由天
4楼-- · 2020-04-10 02:21

If this is something you really, really want, you can add it with QUnit.extend():

QUnit.extend(QUnit.assert, {
    notOk: function (result, message) {
        message = message || (!result ? "okay" : "failed, expected argument to be falsey, was: " +
        QUnit.dump.parse(result));
        QUnit.push(!result, result, false, message);
    },
});
查看更多
叼着烟拽天下
5楼-- · 2020-04-10 02:22

According to the documentation :

The most basic assertion in QUnit, ok() requires just one argument. If the argument evaluates to true, the assertion passes; otherwise, it fails.

You can verify that a method return a false value by writing an expression which evaluates to a true value in the case the method returns false, and vice versa. The easiest expression to do this is the NOT operator, which in JavaScript is expressed through !

test( "Test method returns false ", function() {
  ok( method() == false, "Method returned false" );
  // or using a the negation operator
  ok( !method(), "Method returned false" );
});
查看更多
仙女界的扛把子
6楼-- · 2020-04-10 02:23

You could use: ok(!method_expected_to_be_false)

查看更多
登录 后发表回答