AndrewPiterov / shouldly

MIT License
3 stars 0 forks source link

Assertion doesn't work on nullable variables #4

Open windybranch opened 3 weeks ago

windybranch commented 3 weeks ago

With a basic example:

void main() {
  test("str is null", () {
    String? str;
    str.should.not.beNull;
  });
}

This test passes, whereas a normal expect(str, isNotNull) will fail correctly.

AndrewPiterov commented 3 weeks ago

Basic tests:

 test('string should be null', () {
    const String? str = null;
    str.should.beNull();
});

test('string should not be null', () {
    const String str = 'some string';
    str.should.not.beNull();
});

In your example, you don't add brackets. Please remember that all assertions should end with methods.

windybranch commented 3 weeks ago

You're right! My mistake on that example. It seems I need a more complicated one to illustrate the actual issue I'm having:

class Foo {
  final String id;

  Foo(this.id);
}

class Bar {
  Foo? foo;
}

test("foo id should be null", () {
  final bar = Bar();
  bar.foo?.id.should.not.beNull(); // passes
  expect(bar.foo?.id, isNotNull); // fails

  bar.foo?.id.should.be("id"); // passes as well
});