Permalink
Browse files

feature: Mdlmock works with dice

  • Loading branch information...
MikeMitterer committed May 9, 2017
1 parent 49d5c71 commit 4a892654e87422be5045d8078eea00601cacf50f
View
@@ -64,32 +64,12 @@ class _MdlInjector {
final ClosureMirror cm = reflect(function);
final MethodMirror mm = cm.function;
// List args = new List();
// try {
// args = mm.parameters.map( (final ParameterMirror parameter) {
// var metadata = parameter.metadata;
//
// di.Key key = new di.Key(
// (parameter.type as ClassMirror).reflectedType,
// metadata.isEmpty ? null : metadata.first.type.reflectedType);
//
// return _injector.getByKey(key);
// }).toList();
//
//
//
// } on di_errors.NoProviderError catch(error) {
// print(error);
// }
final List args = mm.parameters.map( (final ParameterMirror parameter) {
var metadata = parameter.metadata;
di.Key key = new di.Key(
(parameter.type as ClassMirror).reflectedType,
metadata.isEmpty ? null : metadata.first.type.reflectedType);
final Type reflectedType = (parameter.type as ClassMirror).reflectedType;
return _injector.getInstance(reflectedType);
return _injector.getByKey(key);
}).toList();
return cm.apply(args).reflectee;
@@ -107,7 +87,7 @@ class _MdlInjector {
class MdlMockModule extends di.Module {
MdlMockModule() {
configure() {
}
}
@@ -120,16 +100,13 @@ void setUpInjector() {
_mdlInjector.addModule(new MdlModule());
}
void module(void regFunction(final di.Module module)) {
final di.Module mod = new di.Module();
regFunction(mod);
void module(final di.Module module) {
if(_mdlInjector == null) {
setUpInjector();
}
_mdlInjector.addModule(mod);
_mdlInjector.addModule(module);
}
void inject(Function fn) {
@@ -43,3 +43,4 @@ main() async {
}
// - Helper --------------------------------------------------------------------------------------
@@ -26,6 +26,7 @@ import "package:mdl/mdl.dart";
import '../config.dart';
main() {
// final Logger _logger = new Logger("test.Form");
configLogging();
@@ -45,3 +45,4 @@ main() async {
}
// - Helper --------------------------------------------------------------------------------------
@@ -45,3 +45,4 @@ main() async {
}
// - Helper --------------------------------------------------------------------------------------
@@ -64,3 +64,4 @@ main() async {
}
// - Helper --------------------------------------------------------------------------------------
@@ -2,7 +2,7 @@
import 'dart:async';
import 'package:test/test.dart';
import 'package:di/di.dart' as di;
import 'package:dice/dice.dart' as di;
import 'package:mdl/mdlmock.dart' as mdlmock;
// import 'package:logging/logging.dart';
@@ -29,6 +29,27 @@ class MockedService extends SimpleService{
}
}
class AnotherMockedService extends SimpleService {
@override
Future<String> connectToServer(final TestCallback callback) {
return new Future<String>.delayed(new Duration(milliseconds: 500),() {
return callback("mocked2-${name}");
});
}
}
class MockModule extends di.Module {
configure() {
register(SimpleService);
}
}
class MockModule2 extends di.Module {
configure() {
register(SimpleService).toType(MockedService);
}
}
main() async {
// final Logger _logger = new Logger("test.Mock");
@@ -37,35 +58,57 @@ main() async {
group('Injection', () {
setUp(() {
mdlmock.setUpInjector();
mdlmock.module((final di.Module module) {
module.bind(SimpleService);
});
mdlmock.module(new MockModule());
});
test('> Simple injection', () async {
final callback = expectAsync1((final String name) => name.toUpperCase());
mdlmock.inject((final SimpleService service) async {
expect(service, isNotNull);
final SimpleService service = mdlmock.injector().getInstance(SimpleService);
expect(service, isNotNull);
final String result = await service.connectToServer(callback);
expect(result,"SIMPLE");
});
final String result = await service.connectToServer(callback);
expect(result,"SIMPLE");
// mdlmock.inject((final SimpleService service) async {
// expect(service, isNotNull);
//
// final String result = await service.connectToServer(callback);
// expect(result,"SIMPLE");
// });
}); // end of 'Simple injection' test
});
// End of 'Injection' group
group('Mock', () {
group('Registration', () {
setUp(() {
mdlmock.setUpInjector();
mdlmock.module((final di.Module module) {
});
module.bind(SimpleService,toImplementation: MockedService);
});
test('> Simple via register', () async {
mdlmock.injector().register(SimpleService).toType(AnotherMockedService);
final callback = expectAsync1((final String name) => name.toUpperCase());
final SimpleService service = mdlmock.injector().getInstance(SimpleService);
expect(service, isNotNull);
final String result = await service.connectToServer(callback);
expect(result,startsWith("MOCKED2-"));
expect(0, equals(0));
}); // end of 'Simple via register' test
}); // End of '' group
group('Mock', () {
setUp(() {
mdlmock.setUpInjector();
mdlmock.module(new MockModule2());
});
test('> Mocked injection', () async {

0 comments on commit 4a89265

Please sign in to comment.