diff --git a/lib/service/engine_service.dart b/lib/service/engine_service.dart index d7b0bed..f43cd55 100644 --- a/lib/service/engine_service.dart +++ b/lib/service/engine_service.dart @@ -5,10 +5,10 @@ import 'package:rules_of_living/src/Engine.dart'; class EngineService { Engine _engine; - Engine get engine => _engine ?? createEngine(null); + Engine get engine => _engine ?? getEngine(Engine()); - Engine createEngine(html.CanvasElement canvas) { - _engine = Engine(canvas); + Engine getEngine(Engine engine) { + _engine = engine; return _engine; } @@ -45,5 +45,4 @@ class EngineService { } bool get isRunning => engine.running; - } diff --git a/test/service/engine_service_test.dart b/test/service/engine_service_test.dart index 9d4a226..209fd47 100644 --- a/test/service/engine_service_test.dart +++ b/test/service/engine_service_test.dart @@ -1,19 +1,41 @@ @TestOn('browser') - import 'package:test/test.dart'; +import 'package:mockito/mockito.dart'; +import 'package:rules_of_living/src/Engine.dart'; import 'package:rules_of_living/service/engine_service.dart'; +class MockEngine extends Mock implements Engine {} + void main() { EngineService sut; + MockEngine me; setUp(() { + me = MockEngine(); sut = EngineService(); }); + group("Dependency Injection", () { + test("EngineService accesses the Engine defined in getEngine", () { + sut.getEngine(me); - test("EngineService creates an engine on demand", () { - expect(sut.engine, isNotNull); + Engine result = sut.engine; + expect(result, equals(me)); + }); }); + group("caching", () { + test("EngineService creates an engine on demand", () { + Engine result = sut.engine; + expect(result, TypeMatcher()); + }); - test("EngineService returns the cached engine on subsequent requests", () { - expect(sut.engine, allOf(isNotNull, equals(sut.engine))); + test("EngineService returns the cached engine on subsequent requests", () { + Engine result = sut.engine; + expect(sut.engine, equals(result)); + }); + test("caching can be overriden by providing a custom engine", () { + Engine first = sut.engine; + sut.getEngine(me); + Engine second = sut.engine; + expect(second, isNot(equals(first))); + }); }); }