platform/packages/container/angel_container/test/named_test.dart
thomashii@dukefirehawk.com 5db88c8a92 Publish angel3_container
2021-05-14 15:41:25 +08:00

34 lines
796 B
Dart

import 'package:angel3_container/angel3_container.dart';
import 'package:test/test.dart';
void main() {
late Container container;
setUp(() {
container = Container(const EmptyReflector());
container.registerNamedSingleton('foo', Foo(bar: 'baz'));
});
test('fetch by name', () {
expect(container.findByName<Foo>('foo')!.bar, 'baz');
});
test('cannot redefine', () {
expect(() => container.registerNamedSingleton('foo', Foo(bar: 'quux')),
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 {
final String? bar;
Foo({this.bar});
}