{"_id":"5447ea600319802200fc06e3","project":"54471fc9e12a270800028adc","category":{"_id":"5447be130319802200fc0620","project":"54471fc9e12a270800028adc","version":"54471fc9e12a270800028adf","__v":6,"pages":["5447be448d7af31a00dd406a","5447ea410319802200fc06df","5447ea600319802200fc06e3","5447ec908d7af31a00dd4115","5447ef6c0319802200fc070f","54485158c1b42e08005b82a1"],"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-22T14:24:19.959Z","from_sync":false,"order":1,"slug":"how-do-i","title":"How do I...?"},"version":{"_id":"54471fc9e12a270800028adf","__v":10,"project":"54471fc9e12a270800028adc","createdAt":"2014-10-22T03:08:57.750Z","releaseDate":"2014-10-22T03:08:57.750Z","categories":["54471fc9e12a270800028ae0","5447b9e7b96a63140077d747","5447be130319802200fc0620","5447ed118d7af31a00dd411c","5447ed230319802200fc0702","5448524c4544c30800241f41","544854504544c30800241f4d","544854af4544c30800241f50","544854e74544c30800241f51","54485557c1b42e08005b82bf"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"__v":4,"user":"54471f91beb6320800da6f75","is_link":false,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2014-10-22T17:33:20.598Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"try":true,"basic_auth":false,"auth":"never","params":[],"url":""},"isReference":false,"order":2,"body":"SpecsFor includes an auto-mocking container that will automatically create mock objects for you using the excellent [Moq](https://github.com/Moq/moq4) library.  By default, mock objects have no behavior.  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      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNow let's look at the specs for building a muscle car:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\t\\tpublic class when_creating_a_muscle_car : SpecsFor<CarFactory>\\n\\t\\t{\\n\\t\\t\\tprivate Car _car;\\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_it_creates_a_car_with_an_engine()\\n\\t\\t\\t{\\n\\t\\t\\t\\t_car.Engine.ShouldNotBeNull();\\n\\t\\t\\t}\\n\\t\\t}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nOur CarFactory will be given a mock object, and when our factory calls its GetEngine method, the mock will happily hand back... null!\n\nTo fix this, we need to configure our mock with some behavior.  We can ask SpecsFor to give us the mock object for a type by calling the GetMockFor<T> method.  Typically, you would perform this sort of setup by overridding the Given method, like so:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\t\\tpublic class when_creating_a_muscle_car : SpecsFor<CarFactory>\\n\\t\\t{\\n\\t\\t\\tprivate Car _car;\\n\\n\\t\\t\\tprotected override void Given()\\n\\t\\t\\t{\\n\\t\\t\\t\\tGetMockFor<IEngineFactory>()\\n\\t\\t\\t\\t\\t.Setup(x => x.GetEngine(\\\"V8\\\"))\\n\\t\\t\\t\\t\\t.Returns(new Engine());\\n\\t\\t\\t}\\n\\n\\t\\t\\t//snip\\n\\t\\t}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNow the spec will pass!\n\nYou can also verify that a method was called on a mock, like so:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\t\\tpublic class when_creating_a_muscle_car : SpecsFor<CarFactory>\\n\\t\\t{\\n\\t\\t\\t//snip\\n\\n\\t\\t\\t[Test]\\n\\t\\t\\tpublic void then_it_calls_the_engine_factory()\\n\\t\\t\\t{\\n\\t\\t\\t\\tGetMockFor<IEngineFactory>()\\n\\t\\t\\t\\t\\t.Verify(x => x.GetEngine(\\\"V8\\\"));\\n\\t\\t\\t}\\n\\t\\t}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nFor more information about mocking, check out [SpecsFor and mocking](doc:specsfor-and-mocking)!","excerpt":"","slug":"set-up-a-mock-objection","type":"basic","title":"Set up a mock object"}

Set up a mock object


SpecsFor includes an auto-mocking container that will automatically create mock objects for you using the excellent [Moq](https://github.com/Moq/moq4) library. By default, mock objects have no behavior. 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}", "language": "csharp" } ] } [/block] Now let's look at the specs for building a muscle car: [block:code] { "codes": [ { "code": "\t\tpublic class when_creating_a_muscle_car : SpecsFor<CarFactory>\n\t\t{\n\t\t\tprivate Car _car;\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_it_creates_a_car_with_an_engine()\n\t\t\t{\n\t\t\t\t_car.Engine.ShouldNotBeNull();\n\t\t\t}\n\t\t}", "language": "csharp" } ] } [/block] Our CarFactory will be given a mock object, and when our factory calls its GetEngine method, the mock will happily hand back... null! To fix this, we need to configure our mock with some behavior. We can ask SpecsFor to give us the mock object for a type by calling the GetMockFor<T> method. Typically, you would perform this sort of setup by overridding the Given method, like so: [block:code] { "codes": [ { "code": "\t\tpublic class when_creating_a_muscle_car : SpecsFor<CarFactory>\n\t\t{\n\t\t\tprivate Car _car;\n\n\t\t\tprotected override void Given()\n\t\t\t{\n\t\t\t\tGetMockFor<IEngineFactory>()\n\t\t\t\t\t.Setup(x => x.GetEngine(\"V8\"))\n\t\t\t\t\t.Returns(new Engine());\n\t\t\t}\n\n\t\t\t//snip\n\t\t}", "language": "csharp" } ] } [/block] Now the spec will pass! You can also verify that a method was called on a mock, like so: [block:code] { "codes": [ { "code": "\t\tpublic class when_creating_a_muscle_car : SpecsFor<CarFactory>\n\t\t{\n\t\t\t//snip\n\n\t\t\t[Test]\n\t\t\tpublic void then_it_calls_the_engine_factory()\n\t\t\t{\n\t\t\t\tGetMockFor<IEngineFactory>()\n\t\t\t\t\t.Verify(x => x.GetEngine(\"V8\"));\n\t\t\t}\n\t\t}", "language": "csharp" } ] } [/block] For more information about mocking, check out [SpecsFor and mocking](doc:specsfor-and-mocking)!