Last active
September 9, 2022 18:25
-
-
Save rafdls/70e0ba3dcde84e91fa7dc17a6937a223 to your computer and use it in GitHub Desktop.
Code snippets for how to write unit tests in flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test('testing future callback', () { | |
int number = 10; | |
Future<int> futureNumber = Future.value(number); | |
futureNumber.then((val) { | |
expect(val, 10); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test('testing future values', () async { | |
int number = 10; | |
Future<int> futureNumber = Future.value(number); | |
expect(await futureNumber, 10); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
group('Dart integers', () { | |
test('should be summed correctly', () { | |
expect(1 + 3, 4); | |
}); | |
test('should be multiplied correctly', () { | |
expect(6 * 2, 12); | |
}); | |
test('should work with modulo operator', () { | |
expect(11 % 5, 1); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter_test/flutter_test.dart'; | |
void main() { | |
test('hello world test', () { | |
int myNumber = 8; | |
expect(myNumber, 8); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
group('Dart integers', () { | |
int intToTest; | |
setUp(() { | |
intToTest = 0; | |
}); | |
test('should increment correctly', () { | |
expect(++intToTest, 1); | |
}); | |
test('should decrement correctly', () { | |
expect(--intToTest, -1); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test('testing streams', () { | |
Stream<String> pineappleStream() async* { | |
yield "fresh pineapple"; | |
yield "another fresh pineapple"; | |
} | |
pineappleStream(); | |
expectLater(pineappleStream(), | |
emitsInOrder(["fresh pineapple", "another fresh pineapple"])); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test('iterable matchers', () { | |
List numbers = [1, 2, 3, 4, 5, 10]; | |
expect(numbers, anyElement(4)); | |
expect(numbers, containsAll([2, 1, 3, 10, 5, 4])); | |
expect(numbers, containsAllInOrder([1, 2, 3, 4, 5, 10])); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test('string matchers', () { | |
expect('the_value', 'the_value'); | |
expect('the_value', startsWith('the')); | |
expect('the_value', endsWith('value')); | |
expect(' value ', equalsIgnoringWhitespace('value')); | |
expect('The_Value', equalsIgnoringCase('the_value')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment