Skip to content

Instantly share code, notes, and snippets.

@MelbourneDeveloper
Last active February 10, 2023 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MelbourneDeveloper/469d64c84869bb8efbdc5aa6389da5a5 to your computer and use it in GitHub Desktop.
Save MelbourneDeveloper/469d64c84869bb8efbdc5aa6389da5a5 to your computer and use it in GitHub Desktop.
Integration Vs. Unit Tests
import 'package:test/test.dart';
typedef NumberFunction = int Function(int);
int addTwo(int original) => original + 2;
int multiplyByTwo(int original) => original * 2;
int subtractThree(int original) => original - 3;
int calculate(
int original,
NumberFunction addTwo,
NumberFunction multiplyByTwo,
NumberFunction subtractThree,
) =>
subtractThree(
multiplyByTwo(
addTwo(original),
),
);
void main() {
//---------------------------------------------------------------
// No isolation
test('Integration test', () {
expect(calculate(2, addTwo, multiplyByTwo, subtractThree), 5);
expect(calculate(5, addTwo, multiplyByTwo, subtractThree), 11);
});
//---------------------------------------------------------------
//---------------------------------------------------------------
//Complete Isolation
test('Unit Test addTwo', () {
expect(addTwo(4), 6);
expect(addTwo(5), 7);
});
test('Unit Test subtractThree', () {
expect(subtractThree(8), 5);
expect(subtractThree(14), 11);
});
test('Unit Test multiplyByTwo', () {
expect(multiplyByTwo(4), 8);
expect(multiplyByTwo(7), 14);
});
test('Unit Test calculate', () {
//These are test doubles to isolate the calculate function
expect(calculate(2, (n) => 6, (n) => 8, (n) => 5), 5);
});
//---------------------------------------------------------------
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment