Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test not equal with matcher in flutter

I'm doing testing on render objects in Flutter. I'd like to check for inequality like this (simplified):

testWidgets('render object heights not equal', (WidgetTester tester) async {    final renderObjectOneHeight = 10;   final renderObjectTwoHeight = 11;    expect(renderObjectOneHeight, notEqual(renderObjectTwoHeight)); }); 

I made up notEqual because it doesn't exist. This doesn't work either:

  • !equals

I found a solution that works so I am posting my answer below Q&A style. I welcome any better solutions, though.

like image 561
Suragch Avatar asked Nov 29 '19 08:11

Suragch


1 Answers

You can use isNot() to negate the equals() matcher.

final x = 1; final y = 2;  expect(x, isNot(equals(y))); 

Or as mentioned in the comments:

expect(x != y, true) 

That actually seems a bit more readable to me.

like image 98
Suragch Avatar answered Sep 17 '22 18:09

Suragch