{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"never","basic_auth":false,"params":[],"results":{"codes":[]},"try":true},"next":{"description":"","pages":[]},"title":"About auto-mocking","type":"basic","slug":"about-auto-mocking","excerpt":"","body":"SpecsFor includes an auto-mocking container.  What's that, you ask?  It's a special Inversion of Control container that resolves dependencies using mock objects.  \n\nAt test-time, when SpecsFor creates your [System Under Test (SUT)](doc:the-system-under-test-sut), it does so using its auto-mocking container.  Any dependencies that your SUT has are resolved (recursively) using the container.  If a dependency is mockable, meaning it is either an abstract type or or an interface type, the container will create a new mock object using the Moq framework, then it will inject that mock object for you automatically.  \n\nConceptually, you can think of the auto-mocking container as doing the following for you:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"SUT = new YourTargetClass(new Mock<IService1>().Object, new Mock<IService2>().Object);\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nThis mocking occurs recursively, too.  If your class depends on a concrete type, the container will create an instance of the concrete type.  If that concrete type has a dependency on a mockable type though, it will inject mock objects for those dependencies.\n\nYou can retrieve any mock object that has been created by the auto-mocking container by calling the GetMockFor<T> method on the SpecsFor base class.  \n\nFor more information check out [how to set up a mock object](doc:set-up-a-mock-objection) and [how to verify a method was called](doc:verify-a-method-was-called-on-another-object).  You can also reconfigure the behavior of the auto-mocking container, such as [registering a concrete type](doc:register-a-concrete-type-in-the-auto-mocking-conta).","updates":[],"order":4,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"5447ef7a8d7af31a00dd4125","is_link":false,"createdAt":"2014-10-22T17:55:06.078Z","githubsync":"","project":"54471fc9e12a270800028adc","user":"54471f91beb6320800da6f75","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"},"category":{"sync":{"isSync":false,"url":""},"pages":["544853a8c1b42e08005b82b6","5447ecac8d7af31a00dd4119","5447ed6d0319802200fc0705","5447ef7a8d7af31a00dd4125","54485217c1b42e08005b82a6","5448536e4544c30800241f48","54485396c1b42e08005b82b2","547c7c5f78fd57080023c99c"],"title":"Basics","slug":"basics","order":2,"from_sync":false,"reference":false,"_id":"544854e74544c30800241f51","project":"54471fc9e12a270800028adc","version":"54471fc9e12a270800028adf","createdAt":"2014-10-23T01:07:51.270Z","__v":1},"__v":3}

About auto-mocking


SpecsFor includes an auto-mocking container. What's that, you ask? It's a special Inversion of Control container that resolves dependencies using mock objects. At test-time, when SpecsFor creates your [System Under Test (SUT)](doc:the-system-under-test-sut), it does so using its auto-mocking container. Any dependencies that your SUT has are resolved (recursively) using the container. If a dependency is mockable, meaning it is either an abstract type or or an interface type, the container will create a new mock object using the Moq framework, then it will inject that mock object for you automatically. Conceptually, you can think of the auto-mocking container as doing the following for you: [block:code] { "codes": [ { "code": "SUT = new YourTargetClass(new Mock<IService1>().Object, new Mock<IService2>().Object);", "language": "csharp" } ] } [/block] This mocking occurs recursively, too. If your class depends on a concrete type, the container will create an instance of the concrete type. If that concrete type has a dependency on a mockable type though, it will inject mock objects for those dependencies. You can retrieve any mock object that has been created by the auto-mocking container by calling the GetMockFor<T> method on the SpecsFor base class. For more information check out [how to set up a mock object](doc:set-up-a-mock-objection) and [how to verify a method was called](doc:verify-a-method-was-called-on-another-object). You can also reconfigure the behavior of the auto-mocking container, such as [registering a concrete type](doc:register-a-concrete-type-in-the-auto-mocking-conta).