platform/packages/container/angel_container/test/named_test.dart

35 lines
794 B
Dart
Raw Normal View History

2018-10-22 15:41:59 +00:00
import 'package:angel_container/angel_container.dart';
import 'package:test/test.dart';
void main() {
2021-03-18 00:21:42 +00:00
late Container container;
2018-10-22 15:41:59 +00:00
setUp(() {
2019-10-12 13:34:15 +00:00
container = Container(const EmptyReflector());
container.registerNamedSingleton('foo', Foo(bar: 'baz'));
2018-10-22 15:41:59 +00:00
});
test('fetch by name', () {
2021-03-18 00:21:42 +00:00
expect(container.findByName<Foo>('foo')!.bar, 'baz');
2018-10-22 15:41:59 +00:00
});
test('cannot redefine', () {
2019-10-12 13:34:15 +00:00
expect(() => container.registerNamedSingleton('foo', Foo(bar: 'quux')),
2018-10-22 15:41:59 +00:00
throwsStateError);
});
test('throws on unknown name', () {
expect(() => container.findByName('bar'), throwsStateError);
});
test('throws on incorrect type', () {
expect(() => container.findByName<List<String>>('foo'), throwsA(anything));
});
}
class Foo {
2021-03-18 00:21:42 +00:00
final String? bar;
2018-10-22 15:41:59 +00:00
Foo({this.bar});
}