Skip to content

Instantly share code, notes, and snippets.

@rafdls
Last active September 9, 2022 18:25
Show Gist options
  • Save rafdls/70e0ba3dcde84e91fa7dc17a6937a223 to your computer and use it in GitHub Desktop.
Save rafdls/70e0ba3dcde84e91fa7dc17a6937a223 to your computer and use it in GitHub Desktop.
Code snippets for how to write unit tests in flutter
test('testing future callback', () {
int number = 10;
Future<int> futureNumber = Future.value(number);
futureNumber.then((val) {
expect(val, 10);
});
});
test('testing future values', () async {
int number = 10;
Future<int> futureNumber = Future.value(number);
expect(await futureNumber, 10);
});
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);
});
});
import 'package:flutter_test/flutter_test.dart';
void main() {
test('hello world test', () {
int myNumber = 8;
expect(myNumber, 8);
});
}
group('Dart integers', () {
int intToTest;
setUp(() {
intToTest = 0;
});
test('should increment correctly', () {
expect(++intToTest, 1);
});
test('should decrement correctly', () {
expect(--intToTest, -1);
});
});
test('testing streams', () {
Stream<String> pineappleStream() async* {
yield "fresh pineapple";
yield "another fresh pineapple";
}
pineappleStream();
expectLater(pineappleStream(),
emitsInOrder(["fresh pineapple", "another fresh pineapple"]));
});
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]));
});
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