Permalink
Browse files

test: Mocked injection

  • Loading branch information...
MikeMitterer committed Feb 6, 2017
1 parent f28fc4e commit b4943166d8e80c3ce529bac7f4c460133a65b990
Showing with 106 additions and 5 deletions.
  1. +21 −5 lib/mdlmock.dart
  2. +85 −0 test/unit/mock/Mock_test.dart
View
@@ -36,7 +36,6 @@
/// }
library mdlmock;
//import 'package:logging/logging.dart';
//import 'package:validate/validate.dart';
import 'dart:mirrors';
@@ -59,21 +58,38 @@ class _MdlInjector {
}
}
void inject(Function function) {
inject(final Function function) {
_create();
final ClosureMirror cm = reflect(function);
final MethodMirror mm = cm.function;
final List args = mm.parameters.map( (ParameterMirror parameter) {
// 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);
metadata.isEmpty ? null : metadata.first.type.reflectedType);
return _injector.getByKey(key);
}).toList();
return cm.apply(args).reflectee;
@@ -0,0 +1,85 @@
//@TestOn("content-shell")
import 'dart:async';
import 'package:test/test.dart';
import 'package:di/di.dart' as di;
import 'package:mdl/mdlmock.dart' as mdlmock;
// import 'package:logging/logging.dart';
import '../config.dart';
typedef String TestCallback(final String name);
class SimpleService {
String get name => "simple";
Future<String> connectToServer(final TestCallback callback) {
return new Future<String>.delayed(new Duration(milliseconds: 500),() => callback(name));
}
}
class MockedService extends SimpleService{
@override
Future<String> connectToServer(final TestCallback callback) {
return new Future<String>.delayed(new Duration(milliseconds: 500),() {
return "mocked-" + callback(name);
});
}
}
main() async {
// final Logger _logger = new Logger("test.Mock");
configLogging();
group('Injection', () {
setUp(() {
mdlmock.setUpInjector();
mdlmock.module((final di.Module module) {
module.bind(SimpleService);
});
});
test('> Simple injection', () async {
final callback = expectAsync1((final String name) => name.toUpperCase());
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', () {
setUp(() {
mdlmock.setUpInjector();
mdlmock.module((final di.Module module) {
module.bind(SimpleService,toImplementation: MockedService);
});
});
test('> Mocked injection', () async {
final callback = expectAsync1((final String name) => name.toUpperCase());
mdlmock.inject((final SimpleService service) async {
expect(service, isNotNull);
final String result = await service.connectToServer(callback);
expect(result,"mocked-SIMPLE");
});
}); // end of 'Mocked injection' test
}); // End of '' group
}
// - Helper --------------------------------------------------------------------------------------

0 comments on commit b494316

Please sign in to comment.