runContained custom container test

This commit is contained in:
Tobe O 2018-10-21 04:08:08 -04:00
parent f672b3062f
commit fc76540155
2 changed files with 13 additions and 5 deletions

View file

@ -321,10 +321,10 @@ class Angel extends Routable {
/// Runs with DI, and *always* reflects. Prefer [runContained]. /// Runs with DI, and *always* reflects. Prefer [runContained].
Future runReflected(Function handler, RequestContext req, ResponseContext res, Future runReflected(Function handler, RequestContext req, ResponseContext res,
[Container container]) { [Container container]) {
container ??= req?.container ?? res?.app?.container;
var h = handleContained( var h = handleContained(
handler, handler,
_preContained[handler] = _preContained[handler] = preInject(handler, container.reflector),
preInject(handler, req.app.container.reflector),
container); container);
return new Future.sync(() => h(req, res)); return new Future.sync(() => h(req, res));
// return closureMirror.apply(args).reflectee; // return closureMirror.apply(args).reflectee;

View file

@ -5,6 +5,7 @@ import 'package:angel_container/angel_container.dart';
import 'package:angel_container/mirrors.dart'; import 'package:angel_container/mirrors.dart';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:mock_request/mock_request.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'common.dart'; import 'common.dart';
@ -47,11 +48,18 @@ main() {
test('runContained with custom container', () async { test('runContained with custom container', () async {
var app = new Angel(); var app = new Angel();
var c = new Container(const EmptyReflector()); var c = new Container(const MirrorsReflector());
c.registerSingleton(new Todo(text: 'Hey!')); c.registerSingleton(new Todo(text: 'Hey!'));
var r = await app.runContained((Todo t) => t, null, null, c); app.get('/', (req, res) async {
expect(r, c); return app.runContained((Todo t) => t.text, req, res, c);
});
var rq = new MockHttpRequest('GET', new Uri(path: '/'))..close();
var rs = rq.response;
await new AngelHttp(app).handleRequest(rq);
var text = await rs.transform(utf8.decoder).join();
expect(text, json.encode('Hey!'));
}); });
test("singleton in route", () async { test("singleton in route", () async {