{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"never","try":true,"basic_auth":false,"params":[],"results":{"codes":[]}},"next":{"description":"","pages":[]},"title":"Register a concrete type in the auto-mocking container","type":"basic","slug":"register-a-concrete-type-in-the-auto-mocking-conta","excerpt":"","body":"By default, SpecsFor's [auto-mocking container](doc:about-auto-mocking) will create mock objects for your class's dependencies.  However, you can easily change this behavior to inject your own mock or fake object or to inject a concrete type.  Consider this trivial domain:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\tpublic class CarFactory\\n\\t{\\n\\t\\tprivate readonly IEngineFactory _engineFactory;\\n\\n\\t\\tpublic CarFactory(IEngineFactory engineFactory)\\n\\t\\t{\\n\\t\\t\\t_engineFactory = engineFactory;\\n\\t\\t}\\n\\n\\t\\tpublic Car BuildMuscleCar()\\n\\t\\t{\\n\\t\\t\\treturn new Car\\n\\t\\t\\t{\\n\\t\\t\\t\\tEngine = _engineFactory.GetEngine(\\\"V8\\\")\\n\\t\\t\\t};\\n\\t\\t}\\n\\t}\\n\\n\\tpublic class Car\\n\\t{\\n\\t\\tpublic Engine Engine { get; set; }\\n\\t}\\n\\n\\tpublic class Engine\\n\\t{\\n    public string Maker { get; set; }\\n\\t\\tpublic string Type { get; set; }\\n\\t}\\n\\n\\tpublic interface IEngineFactory\\n\\t{\\n\\t\\tEngine GetEngine(string engineType);\\n\\t}\\n\\n\\tpublic class RealEngineFactory : IEngineFactory\\n\\t{\\n\\t\\tpublic Engine GetEngine(string engineType)\\n\\t\\t{\\n\\t\\t\\treturn new Engine\\n\\t\\t\\t{\\n\\t\\t\\t\\tMaker = \\\"Real Engines, Inc\\\",\\n\\t\\t\\t\\tType = engineType\\n\\t\\t\\t};\\n\\t\\t}\\n\\t}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNow let's say we wanted to write integration specs against our CarFactory, so we want our specs to also use RealEngineFactory.  We can tell SpecsFor to use RealEngineFactory instead of a mock by overriding the ConfigureContainer method:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\t\\t\\tprotected override void ConfigureContainer(IContainer container)\\n\\t\\t\\t{\\n\\t\\t\\t\\tcontainer.Configure(cfg =>\\n\\t\\t\\t\\t{\\n\\t\\t\\t\\t\\tcfg.For<IEngineFactory>().Use<RealEngineFactory>();\\n\\t\\t\\t\\t});\\n\\t\\t\\t}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNow our system under test will receive an instance of RealEngineFactory instead of a mock.  \n\nHere's what our full specs look like:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\tpublic class ContainerConfigurationSpecs\\n\\t{\\n\\t\\tpublic class when_a_concrete_type_is_registered_in_the_container : SpecsFor<CarFactory>\\n\\t\\t{\\n\\t\\t\\tprivate Car _car;\\n\\n\\t\\t\\tprotected override void ConfigureContainer(IContainer container)\\n\\t\\t\\t{\\n\\t\\t\\t\\tcontainer.Configure(cfg =>\\n\\t\\t\\t\\t{\\n\\t\\t\\t\\t\\tcfg.For<IEngineFactory>().Use<RealEngineFactory>();\\n\\t\\t\\t\\t});\\n\\t\\t\\t}\\n\\n\\t\\t\\tprotected override void When()\\n\\t\\t\\t{\\n\\t\\t\\t\\t_car = SUT.BuildMuscleCar();\\n\\t\\t\\t}\\n\\n\\t\\t\\t[Test]\\n\\t\\t\\tpublic void then_the_cars_engine_was_made_by_the_real_engine_factory()\\n\\t\\t\\t{\\n\\t\\t\\t\\t_car.Engine.Maker.ShouldEqual(\\\"Real Engines, Inc\\\");\\n\\t\\t\\t}\\n\\t\\t}\\n\\t}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]","updates":[],"order":4,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"5447ef6c0319802200fc070f","__v":1,"version":{"version":"1.0","version_clean":"1.0.0","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["54471fc9e12a270800028ae0","5447b9e7b96a63140077d747","5447be130319802200fc0620","5447ed118d7af31a00dd411c","5447ed230319802200fc0702","5448524c4544c30800241f41","544854504544c30800241f4d","544854af4544c30800241f50","544854e74544c30800241f51","54485557c1b42e08005b82bf"],"_id":"54471fc9e12a270800028adf","__v":10,"project":"54471fc9e12a270800028adc","releaseDate":"2014-10-22T03:08:57.750Z","createdAt":"2014-10-22T03:08:57.750Z"},"createdAt":"2014-10-22T17:54:52.730Z","is_link":false,"user":"54471f91beb6320800da6f75","category":{"sync":{"isSync":false,"url":""},"pages":["5447be448d7af31a00dd406a","5447ea410319802200fc06df","5447ea600319802200fc06e3","5447ec908d7af31a00dd4115","5447ef6c0319802200fc070f","54485158c1b42e08005b82a1"],"title":"How do I...?","slug":"how-do-i","order":1,"from_sync":false,"reference":false,"_id":"5447be130319802200fc0620","project":"54471fc9e12a270800028adc","version":"54471fc9e12a270800028adf","createdAt":"2014-10-22T14:24:19.959Z","__v":6},"githubsync":"","project":"54471fc9e12a270800028adc"}

Register a concrete type in the auto-mocking container


By default, SpecsFor's [auto-mocking container](doc:about-auto-mocking) will create mock objects for your class's dependencies. However, you can easily change this behavior to inject your own mock or fake object or to inject a concrete type. Consider this trivial domain: [block:code] { "codes": [ { "code": "\tpublic class CarFactory\n\t{\n\t\tprivate readonly IEngineFactory _engineFactory;\n\n\t\tpublic CarFactory(IEngineFactory engineFactory)\n\t\t{\n\t\t\t_engineFactory = engineFactory;\n\t\t}\n\n\t\tpublic Car BuildMuscleCar()\n\t\t{\n\t\t\treturn new Car\n\t\t\t{\n\t\t\t\tEngine = _engineFactory.GetEngine(\"V8\")\n\t\t\t};\n\t\t}\n\t}\n\n\tpublic class Car\n\t{\n\t\tpublic Engine Engine { get; set; }\n\t}\n\n\tpublic class Engine\n\t{\n public string Maker { get; set; }\n\t\tpublic string Type { get; set; }\n\t}\n\n\tpublic interface IEngineFactory\n\t{\n\t\tEngine GetEngine(string engineType);\n\t}\n\n\tpublic class RealEngineFactory : IEngineFactory\n\t{\n\t\tpublic Engine GetEngine(string engineType)\n\t\t{\n\t\t\treturn new Engine\n\t\t\t{\n\t\t\t\tMaker = \"Real Engines, Inc\",\n\t\t\t\tType = engineType\n\t\t\t};\n\t\t}\n\t}", "language": "csharp" } ] } [/block] Now let's say we wanted to write integration specs against our CarFactory, so we want our specs to also use RealEngineFactory. We can tell SpecsFor to use RealEngineFactory instead of a mock by overriding the ConfigureContainer method: [block:code] { "codes": [ { "code": "\t\t\tprotected override void ConfigureContainer(IContainer container)\n\t\t\t{\n\t\t\t\tcontainer.Configure(cfg =>\n\t\t\t\t{\n\t\t\t\t\tcfg.For<IEngineFactory>().Use<RealEngineFactory>();\n\t\t\t\t});\n\t\t\t}", "language": "csharp" } ] } [/block] Now our system under test will receive an instance of RealEngineFactory instead of a mock. Here's what our full specs look like: [block:code] { "codes": [ { "code": "\tpublic class ContainerConfigurationSpecs\n\t{\n\t\tpublic class when_a_concrete_type_is_registered_in_the_container : SpecsFor<CarFactory>\n\t\t{\n\t\t\tprivate Car _car;\n\n\t\t\tprotected override void ConfigureContainer(IContainer container)\n\t\t\t{\n\t\t\t\tcontainer.Configure(cfg =>\n\t\t\t\t{\n\t\t\t\t\tcfg.For<IEngineFactory>().Use<RealEngineFactory>();\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tprotected override void When()\n\t\t\t{\n\t\t\t\t_car = SUT.BuildMuscleCar();\n\t\t\t}\n\n\t\t\t[Test]\n\t\t\tpublic void then_the_cars_engine_was_made_by_the_real_engine_factory()\n\t\t\t{\n\t\t\t\t_car.Engine.Maker.ShouldEqual(\"Real Engines, Inc\");\n\t\t\t}\n\t\t}\n\t}", "language": "csharp" } ] } [/block]