A Dart library that automatically generates Stub factory method.
APACHE-2.0 License
stub_gen
is a Dart package that generates stubs using the build
package. This can facilitate writing unit tests for model logic.
.stub.dart
file where the stub method is written.freezed
.faker
package.Add the following to your pubspec.yaml
:
stub_gen:
git:
url: https://github.com/fummicc1/stub_gen
ref: 0.0.6
StubGen
:// sample.dart
part 'sample.stub.dart';
@StubGen()
class User {
final String name;
final int age;
final double height;
final String email;
const User(
this.name,
this.age,
this.height,
@EmailAddress()
this.email,
);
String get initial {
return name[0].toUpperCase();
}
bool get isValidEmail {
return email.contains("@");
}
}
StubGen
has a defaultValues
parameter that allows you to modify the default values of StubbableTypes
.@EmailAddress
and @PhoneNumber
.build_runner
to generate the stub code:dart run build_runner build
this command generates sample.stub.dart
file like below.
// coverage:ignore-file
// ignore_for_file: type=lint
part of 'sample.dart';
// **************************************************************************
// StubGenerator
// **************************************************************************
extension UserStubBuilder on User {
static User build(
{String name = "stub",
int age = 10,
double height = 1.0,
String email = "[email protected]"}) =>
User(name ?? "stub", age ?? 10, height ?? 1.0, email ?? "stub");
}
import 'package:example/sample.dart';
import 'package:test/test.dart';
void main() {
test("Initial should be capitalized and email should be valid", () {
final user = UserStubBuilder.build(name: 'fummicc1');
expect(user.initial, "F");
expect(user.isValidEmail, isTrue);
});
}