question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to assert null with should

See original GitHub issue

should.be.null throw an error saying should.be doesn’t have this null method.

How to transform the expect(null).to.be.null to should phrases?

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Reactions:7
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

64reactions
logicalparadoxcommented, Jul 25, 2012

Well, you have a couple of options…

var chai = require('chai')
  , should = chai.should()
  , example = null;

I am assuming you are trying the following and its not working…

example.should.be.null;

The should interface extends Object.prototype with the should keyword and since null is not an Object… etc.

Here are a few of the alternatives, given that you have var should = chai.should(); as opposed to just calling chai.should().

// will test example to be equal to either `null` or `undefined`.
should.not.exist(example);

// if you need exactly null
should.equal(example, null);

In most cases the not.exist will likely work for you, especially if you are testing async callbacks in node.

db.get('abc', function (err, doc) {
  should.not.exist(err);
  should.exist(doc);
  // ...
});

Hope that helps!

12reactions
elliotfcommented, Sep 24, 2012
should.be.null(example)

looks nice, too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AssertNull should be used or AssertNotNull - Stack Overflow
The assertNotNull() method means "a passed parameter must not be null ": if it is null then the test case fails. The assertNull()...
Read more >
JUnit assertNull example - Java2Blog
Assertions.assertNull() checks that object is null. In case, object is not null, it will through AssertError. public static void assertNull(Object actual)
Read more >
org.junit.Assert.assertNull java code examples - Tabnine
Asserts that an object isn't null. If it is an AssertionError is thrown with the given message. assertFalse · fail. Fails a test...
Read more >
Assertion method Assert.assertNull() example. - Java2Novice
Assert class provides a set of assertion methods useful for writing tests. Assert.assertNull() methods checks that the object is null or not. If...
Read more >
Expect / Should - Chai Assertion Library
Just because you can negate any assertion with .not doesn't mean you should. With great power comes great responsibility. It's often best to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found