From fc76540155bc30014518267a3dec820fe8fc069e Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sun, 21 Oct 2018 04:08:08 -0400 Subject: [PATCH] runContained custom container test --- lib/src/core/server.dart | 4 ++-- test/di_test.dart | 14 +++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/src/core/server.dart b/lib/src/core/server.dart index 90167f65..1bf3365a 100644 --- a/lib/src/core/server.dart +++ b/lib/src/core/server.dart @@ -321,10 +321,10 @@ class Angel extends Routable { /// Runs with DI, and *always* reflects. Prefer [runContained]. Future runReflected(Function handler, RequestContext req, ResponseContext res, [Container container]) { + container ??= req?.container ?? res?.app?.container; var h = handleContained( handler, - _preContained[handler] = - preInject(handler, req.app.container.reflector), + _preContained[handler] = preInject(handler, container.reflector), container); return new Future.sync(() => h(req, res)); // return closureMirror.apply(args).reflectee; diff --git a/test/di_test.dart b/test/di_test.dart index 9d708273..e9cdbc97 100644 --- a/test/di_test.dart +++ b/test/di_test.dart @@ -5,6 +5,7 @@ import 'package:angel_container/angel_container.dart'; import 'package:angel_container/mirrors.dart'; import 'package:angel_framework/angel_framework.dart'; import 'package:http/http.dart' as http; +import 'package:mock_request/mock_request.dart'; import 'package:test/test.dart'; import 'common.dart'; @@ -47,11 +48,18 @@ main() { test('runContained with custom container', () async { var app = new Angel(); - var c = new Container(const EmptyReflector()); + var c = new Container(const MirrorsReflector()); c.registerSingleton(new Todo(text: 'Hey!')); - var r = await app.runContained((Todo t) => t, null, null, c); - expect(r, c); + app.get('/', (req, res) async { + 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 {