├── .gitignore ├── CHANGELOG.md ├── LICENSE.txt ├── Ninject.MockingKernel.sln ├── README.md ├── appveyor.yml └── src ├── Ninject.MockingKernel.FakeItEasy.Test ├── FakeItEasyIntegrationTest.cs └── Ninject.MockingKernel.FakeItEasy.Test.csproj ├── Ninject.MockingKernel.FakeItEasy ├── FakeItEasyMockProvider.cs ├── FakeItEasyMockingKernel.cs ├── FakeItEasyModule.cs ├── Ninject.MockingKernel.FakeItEasy.csproj └── stylecop.json ├── Ninject.MockingKernel.Moq.Test ├── DefaultMockRepositoryProviderTest.cs ├── MoqIntegrationTest.cs └── Ninject.MockingKernel.Moq.Test.csproj ├── Ninject.MockingKernel.Moq ├── DefaultMockRepositoryProvider.cs ├── IMockRepositoryProvider.cs ├── MoqMockProvider.cs ├── MoqMockingKernel.cs ├── MoqModule.cs ├── Ninject.MockingKernel.Moq.csproj ├── NinjectSettingsExtensions.cs └── stylecop.json ├── Ninject.MockingKernel.NSubstitute.Test ├── NSubstituteIntegrationTest.cs └── Ninject.MockingKernel.NSubstitute.Test.csproj ├── Ninject.MockingKernel.NSubstitute ├── NSubstituteMockProvider.cs ├── NSubstituteMockingKernel.cs ├── NSubstituteModule.cs ├── Ninject.MockingKernel.NSubstitute.csproj └── stylecop.json ├── Ninject.MockingKernel.Test ├── IDummyService.cs ├── IDummyService2.cs ├── IntegrationTest.cs └── Ninject.MockingKernel.Test.csproj ├── Ninject.MockingKernel ├── AdditionalInterfaceParameter.cs ├── ExtensionsForBindingSyntax.cs ├── IMockProviderCallbackProvider.cs ├── MockMissingBindingResolver.cs ├── MockingKernel.cs ├── Ninject.MockingKernel.csproj ├── SingletonSelfBindingResolver.cs └── stylecop.json └── Ninject.snk /.gitignore: -------------------------------------------------------------------------------- 1 | .vs 2 | *.user 3 | bin 4 | obj 5 | TestResults -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 5 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 6 | 7 | ## [3.3.0] - 2017-10-28 8 | 9 | ### Added 10 | - Support auto mocking named interfaces [#17](https://github.com/ninject/Ninject.MockingKernel/issues/17) 11 | 12 | ## [3.3.0-beta1] 13 | 14 | ### Added 15 | - Support .NET Standard 2.0 16 | 17 | ### Removed 18 | - .NET 3.5, .NET 4.0 and Silverlight 19 | - Dropped Ninject.MockingKernel.RhinoMocks 20 | 21 | ## [3.2.2] 22 | 23 | ### Added 24 | - Support FakeItEasy 25 | 26 | ## [3.2.1] 27 | 28 | ### Added 29 | - Support additional interfaces when mock 30 | 31 | ## [3.0.0.0] 32 | 33 | ### Removed 34 | - No web builds. All builds are have not reference to web anymore 35 | 36 | ### Added 37 | - (Moq): Support for for strict mocks 38 | - (Moq): The MockRepository can be accessed using kernel.MockRepository -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Ninject is intended to be used in both open-source and commercial environments. To allow its use in as many 2 | situations as possible, Ninject is dual-licensed. You may choose to use Ninject under either the Apache License, 3 | Version 2.0, or the Microsoft Public License (Ms-PL). These licenses are essentially identical, but you are 4 | encouraged to evaluate both to determine which best fits your intended use. 5 | 6 | ----- 7 | 8 | Apache License, Version 2.0 9 | 10 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 11 | 12 | 1. Definitions. 13 | 14 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 15 | through 9 of this document. 16 | 17 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. 18 | 19 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, 20 | or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, 21 | direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or 22 | (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source 27 | code, documentation source, and configuration files. 28 | 29 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, 30 | including but not limited to compiled object code, generated documentation, and conversions to other media types. 31 | 32 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, 33 | as indicated by a copyright notice that is included in or attached to the work (an example is provided in the 34 | Appendix below). 35 | 36 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) 37 | the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, 38 | as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include 39 | works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative 40 | Works thereof. 41 | 42 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications 43 | or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion 44 | in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the 45 | copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written 46 | communication sent to the Licensor or its representatives, including but not limited to communication on electronic 47 | mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, 48 | the Licensor for the purpose of discussing and improving the Work, but excluding communication that is 49 | conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." 50 | 51 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been 52 | received by Licensor and subsequently incorporated within the Work. 53 | 54 | 2. Grant of Copyright License. 55 | 56 | Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, 57 | non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, 58 | publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or 59 | Object form. 60 | 61 | 3. Grant of Patent License. 62 | 63 | Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, 64 | non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, 65 | have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to 66 | those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone 67 | or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You 68 | institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging 69 | that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent 70 | infringement, then any patent licenses granted to You under this License for that Work shall terminate as of 71 | the date such litigation is filed. 72 | 73 | 4. Redistribution. 74 | 75 | You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without 76 | modifications, and in Source or Object form, provided that You meet the following conditions: 77 | 78 | 1. You must give any other recipients of the Work or Derivative Works a copy of this License; and 79 | 2. You must cause any modified files to carry prominent notices stating that You changed the files; and 80 | 3. You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, 81 | trademark, and attribution notices from the Source form of the Work, excluding those notices that do not 82 | pertain to any part of the Derivative Works; and 83 | 4. If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You 84 | distribute must include a readable copy of the attribution notices contained within such NOTICE file, 85 | excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the 86 | following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source 87 | form or documentation, if provided along with the Derivative Works; or, within a display generated by the 88 | Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file 89 | are for informational purposes only and do not modify the License. You may add Your own attribution notices 90 | within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, 91 | provided that such additional attribution notices cannot be construed as modifying the License. 92 | 5. You may add Your own copyright statement to Your modifications and may provide additional or different license 93 | terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative 94 | Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the 95 | conditions stated in this License. 96 | 97 | 5. Submission of Contributions. 98 | 99 | Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You 100 | to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. 101 | Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you 102 | may have executed with Licensor regarding such Contributions. 103 | 104 | 6. Trademarks. 105 | 106 | This License does not grant permission to use the trade names, trademarks, service marks, or product names of the 107 | Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing 108 | the content of the NOTICE file. 109 | 110 | 7. Disclaimer of Warranty. 111 | 112 | Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides 113 | its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, 114 | including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS 115 | FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing 116 | the Work and assume any risks associated with Your exercise of permissions under this License. 117 | 118 | 8. Limitation of Liability. 119 | 120 | In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless 121 | required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any 122 | Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential 123 | damages of any character arising as a result of this License or out of the use or inability to use the Work 124 | (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, 125 | or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility 126 | of such damages. 127 | 128 | 9. Accepting Warranty or Additional Liability. 129 | 130 | While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, 131 | acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this 132 | License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole 133 | responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold 134 | each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason 135 | of your accepting any such warranty or additional liability. 136 | 137 | ----- 138 | 139 | Microsoft Public License (Ms-PL) 140 | 141 | This license governs use of the accompanying software. If you use the software, you 142 | accept this license. If you do not accept the license, do not use the software. 143 | 144 | 1. Definitions 145 | The terms "reproduce," "reproduction," "derivative works," and "distribution" have the 146 | same meaning here as under U.S. copyright law. 147 | A "contribution" is the original software, or any additions or changes to the software. 148 | A "contributor" is any person that distributes its contribution under this license. 149 | "Licensed patents" are a contributor's patent claims that read directly on its contribution. 150 | 151 | 2. Grant of Rights 152 | (A) Copyright Grant- Subject to the terms of this license, including the license conditions and 153 | limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free 154 | copyright license to reproduce its contribution, prepare derivative works of its contribution, 155 | and distribute its contribution or any derivative works that you create. 156 | (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations 157 | in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under 158 | its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose 159 | of its contribution in the software or derivative works of the contribution in the software. 160 | 161 | 3. Conditions and Limitations 162 | (A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, 163 | or trademarks. 164 | (B) If you bring a patent claim against any contributor over patents that you claim are infringed by 165 | the software, your patent license from such contributor to the software ends automatically. 166 | (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, 167 | and attribution notices that are present in the software. 168 | (D) If you distribute any portion of the software in source code form, you may do so only under this 169 | license by including a complete copy of this license with your distribution. If you distribute 170 | any portion of the software in compiled or object code form, you may only do so under a license 171 | that complies with this license. 172 | (E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express 173 | warranties, guarantees or conditions. You may have additional consumer rights under your local laws 174 | which this license cannot change. To the extent permitted under your local laws, the contributors 175 | exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement. -------------------------------------------------------------------------------- /Ninject.MockingKernel.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26730.16 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{19E2834E-3CD3-420D-ADE1-FC9A75E67E04}" 7 | ProjectSection(SolutionItems) = preProject 8 | appveyor.yml = appveyor.yml 9 | CHANGELOG.md = CHANGELOG.md 10 | README.md = README.md 11 | EndProjectSection 12 | EndProject 13 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ninject.MockingKernel", "src\Ninject.MockingKernel\Ninject.MockingKernel.csproj", "{C0CFA4C0-1B4D-4011-94F2-7DB1CF98D67D}" 14 | EndProject 15 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ninject.MockingKernel.Test", "src\Ninject.MockingKernel.Test\Ninject.MockingKernel.Test.csproj", "{40A61F8C-1982-45A8-9DDB-9509F41DB6D7}" 16 | EndProject 17 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ninject.MockingKernel.Moq", "src\Ninject.MockingKernel.Moq\Ninject.MockingKernel.Moq.csproj", "{E2AB8BFC-08D4-4996-A034-8FDE4704C9A3}" 18 | EndProject 19 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ninject.MockingKernel.Moq.Test", "src\Ninject.MockingKernel.Moq.Test\Ninject.MockingKernel.Moq.Test.csproj", "{3644F8BE-C266-4236-813C-B7056E91200B}" 20 | EndProject 21 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ninject.MockingKernel.NSubstitute", "src\Ninject.MockingKernel.NSubstitute\Ninject.MockingKernel.NSubstitute.csproj", "{5C36A6ED-BFD9-4181-96ED-FED4D0E1E6F5}" 22 | EndProject 23 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ninject.MockingKernel.NSubstitute.Test", "src\Ninject.MockingKernel.NSubstitute.Test\Ninject.MockingKernel.NSubstitute.Test.csproj", "{DFCD85F9-31D7-4DDF-BEBE-5287B727AF37}" 24 | EndProject 25 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ninject.MockingKernel.FakeItEasy", "src\Ninject.MockingKernel.FakeItEasy\Ninject.MockingKernel.FakeItEasy.csproj", "{6C2C9465-A2FE-46B8-8537-4A81FEEFFB37}" 26 | EndProject 27 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ninject.MockingKernel.FakeItEasy.Test", "src\Ninject.MockingKernel.FakeItEasy.Test\Ninject.MockingKernel.FakeItEasy.Test.csproj", "{026CDA2C-BC44-4F2E-A6AD-EC41B74FA4FD}" 28 | EndProject 29 | Global 30 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 31 | Debug|Any CPU = Debug|Any CPU 32 | Release|Any CPU = Release|Any CPU 33 | EndGlobalSection 34 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 35 | {C0CFA4C0-1B4D-4011-94F2-7DB1CF98D67D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 36 | {C0CFA4C0-1B4D-4011-94F2-7DB1CF98D67D}.Debug|Any CPU.Build.0 = Debug|Any CPU 37 | {C0CFA4C0-1B4D-4011-94F2-7DB1CF98D67D}.Release|Any CPU.ActiveCfg = Release|Any CPU 38 | {C0CFA4C0-1B4D-4011-94F2-7DB1CF98D67D}.Release|Any CPU.Build.0 = Release|Any CPU 39 | {40A61F8C-1982-45A8-9DDB-9509F41DB6D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 40 | {40A61F8C-1982-45A8-9DDB-9509F41DB6D7}.Debug|Any CPU.Build.0 = Debug|Any CPU 41 | {40A61F8C-1982-45A8-9DDB-9509F41DB6D7}.Release|Any CPU.ActiveCfg = Release|Any CPU 42 | {40A61F8C-1982-45A8-9DDB-9509F41DB6D7}.Release|Any CPU.Build.0 = Release|Any CPU 43 | {E2AB8BFC-08D4-4996-A034-8FDE4704C9A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 44 | {E2AB8BFC-08D4-4996-A034-8FDE4704C9A3}.Debug|Any CPU.Build.0 = Debug|Any CPU 45 | {E2AB8BFC-08D4-4996-A034-8FDE4704C9A3}.Release|Any CPU.ActiveCfg = Release|Any CPU 46 | {E2AB8BFC-08D4-4996-A034-8FDE4704C9A3}.Release|Any CPU.Build.0 = Release|Any CPU 47 | {3644F8BE-C266-4236-813C-B7056E91200B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 48 | {3644F8BE-C266-4236-813C-B7056E91200B}.Debug|Any CPU.Build.0 = Debug|Any CPU 49 | {3644F8BE-C266-4236-813C-B7056E91200B}.Release|Any CPU.ActiveCfg = Release|Any CPU 50 | {3644F8BE-C266-4236-813C-B7056E91200B}.Release|Any CPU.Build.0 = Release|Any CPU 51 | {5C36A6ED-BFD9-4181-96ED-FED4D0E1E6F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 52 | {5C36A6ED-BFD9-4181-96ED-FED4D0E1E6F5}.Debug|Any CPU.Build.0 = Debug|Any CPU 53 | {5C36A6ED-BFD9-4181-96ED-FED4D0E1E6F5}.Release|Any CPU.ActiveCfg = Release|Any CPU 54 | {5C36A6ED-BFD9-4181-96ED-FED4D0E1E6F5}.Release|Any CPU.Build.0 = Release|Any CPU 55 | {DFCD85F9-31D7-4DDF-BEBE-5287B727AF37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 56 | {DFCD85F9-31D7-4DDF-BEBE-5287B727AF37}.Debug|Any CPU.Build.0 = Debug|Any CPU 57 | {DFCD85F9-31D7-4DDF-BEBE-5287B727AF37}.Release|Any CPU.ActiveCfg = Release|Any CPU 58 | {DFCD85F9-31D7-4DDF-BEBE-5287B727AF37}.Release|Any CPU.Build.0 = Release|Any CPU 59 | {6C2C9465-A2FE-46B8-8537-4A81FEEFFB37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 60 | {6C2C9465-A2FE-46B8-8537-4A81FEEFFB37}.Debug|Any CPU.Build.0 = Debug|Any CPU 61 | {6C2C9465-A2FE-46B8-8537-4A81FEEFFB37}.Release|Any CPU.ActiveCfg = Release|Any CPU 62 | {6C2C9465-A2FE-46B8-8537-4A81FEEFFB37}.Release|Any CPU.Build.0 = Release|Any CPU 63 | {026CDA2C-BC44-4F2E-A6AD-EC41B74FA4FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 64 | {026CDA2C-BC44-4F2E-A6AD-EC41B74FA4FD}.Debug|Any CPU.Build.0 = Debug|Any CPU 65 | {026CDA2C-BC44-4F2E-A6AD-EC41B74FA4FD}.Release|Any CPU.ActiveCfg = Release|Any CPU 66 | {026CDA2C-BC44-4F2E-A6AD-EC41B74FA4FD}.Release|Any CPU.Build.0 = Release|Any CPU 67 | EndGlobalSection 68 | GlobalSection(SolutionProperties) = preSolution 69 | HideSolutionNode = FALSE 70 | EndGlobalSection 71 | GlobalSection(ExtensibilityGlobals) = postSolution 72 | SolutionGuid = {3C78AEB0-2156-4BB6-BD96-7C0A231B7E05} 73 | EndGlobalSection 74 | EndGlobal 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ninject.MockingKernel 2 | 3 | [![Build status](https://ci.appveyor.com/api/projects/status/kitij4ayps08ot1c?svg=true)](https://ci.appveyor.com/project/Ninject/ninject-mockingkernel) 4 | [![codecov](https://codecov.io/gh/ninject/Ninject.MockingKernel/branch/master/graph/badge.svg)](https://codecov.io/gh/ninject/Ninject.MockingKernel) 5 | [![NuGet Version](http://img.shields.io/nuget/v/Ninject.MockingKernel.svg?style=flat)](https://www.nuget.org/packages/Ninject.MockingKernel/) 6 | [![NuGet Downloads](http://img.shields.io/nuget/dt/Ninject.MockingKernel.svg?style=flat)](https://www.nuget.org/packages/Ninject.MockingKernel/) 7 | 8 | This extension provides integration between Ninject and Moq/NSubstitute/FakeItEasy, creating a "lightweight" auto-mocking container. 9 | 10 | ## Getting started 11 | 12 | In your tests, you should use the MockingKernel instead of the StandardKernel. It adds the following features to Ninject: 13 | 14 | 1. The following syntax will bind a service to the mocked object of a `Mock`. 15 | ```C# 16 | Bind().ToMock(); 17 | ``` 18 | You can add additional interfaces to the mock: 19 | ```C# 20 | Bind().ToMock(typeof(IInterface1), typeof(IInterface2)); 21 | ``` 22 | 23 | 2. If you request a service that has no binding, instead of creating an implicit self-binding, the MockingKernel 24 | will create an instance of `Mock` and return the mocked object associated with it. 25 | 26 | 3. A `Reset()` method is available, which clears the Ninject cache of any activated instances, regardless of whether they 27 | remain in scope. You can call this method after each test to ensure that instances are reactivated, without having 28 | to dispose and re-initialize the kernel each time. 29 | 30 | Hat tip to [Scott Reynolds](http://github.com/scottcreynolds) for the idea, and to [Sean Chambers](http://github.com/schambers) for dogfooding. 31 | 32 | ## Documentation 33 | https://github.com/ninject/Ninject.MockingKernel/wiki -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | configuration: Release 2 | 3 | image: Visual Studio 2017 4 | 5 | init: 6 | - ps: >- 7 | if ($env:APPVEYOR_REPO_TAG -eq "true") 8 | { 9 | Update-AppveyorBuild -Version "$env:APPVEYOR_REPO_TAG_NAME" 10 | } 11 | else 12 | { 13 | Update-AppveyorBuild -Version "3.3.0-ci.$($env:APPVEYOR_BUILD_NUMBER)+sha.$($env:APPVEYOR_REPO_COMMIT.substring(0,7))" 14 | } 15 | 16 | dotnet_csproj: 17 | patch: true 18 | file: '**\*.csproj' 19 | version: '{version}' 20 | package_version: '{version}' 21 | 22 | cache: 23 | - C:\ProgramData\chocolatey\bin -> appveyor.yml 24 | - C:\ProgramData\chocolatey\lib -> appveyor.yml 25 | - '%LocalAppData%\NuGet\v3-cache -> **\*.csproj' 26 | 27 | before_build: 28 | - dotnet restore --verbosity quiet 29 | - choco install opencover.portable 30 | - choco install codecov 31 | 32 | build: 33 | parallel: true 34 | verbosity: minimal 35 | 36 | test_script: 37 | - OpenCover.Console.exe -oldstyle -returntargetcode -register:user -target:"C:\Program Files\dotnet\dotnet.exe" -targetargs:"test --configuration Release --no-build src\Ninject.MockingKernel.FakeItEasy.Test" -filter:"+[Ninject.MockingKernel]* +[Ninject.MockingKernel.FakeItEasy]*" -excludebyattribute:*.ExcludeFromCodeCoverage*;*.Obsolete* -output:"Ninject.MockingKernel.FakeItEasy_coverage.xml" 38 | - OpenCover.Console.exe -oldstyle -returntargetcode -register:user -target:"C:\Program Files\dotnet\dotnet.exe" -targetargs:"test --configuration Release --no-build src\Ninject.MockingKernel.Moq.Test" -filter:"+[Ninject.MockingKernel]* +[Ninject.MockingKernel.Moq]*" -excludebyattribute:*.ExcludeFromCodeCoverage*;*.Obsolete* -output:"Ninject.MockingKernel.Moq_coverage.xml" 39 | - OpenCover.Console.exe -oldstyle -returntargetcode -register:user -target:"C:\Program Files\dotnet\dotnet.exe" -targetargs:"test --configuration Release --no-build src\Ninject.MockingKernel.NSubstitute.Test" -filter:"+[Ninject.MockingKernel]* +[Ninject.MockingKernel.NSubstitute]*" -excludebyattribute:*.ExcludeFromCodeCoverage*;*.Obsolete* -output:"Ninject.MockingKernel.NSubstitute_coverage.xml" 40 | - codecov -f "Ninject.MockingKernel.FakeItEasy_coverage.xml" 41 | - codecov -f "Ninject.MockingKernel.Moq_coverage.xml" 42 | - codecov -f "Ninject.MockingKernel.NSubstitute_coverage.xml" 43 | 44 | artifacts: 45 | - path: '**\*.nupkg' 46 | 47 | deploy: 48 | provider: NuGet 49 | api_key: 50 | secure: Sn05A/FiUAlID5v31uaf5EZuYCSo4frLONtrCY+cwY265dU2JUZsl2+sjVeLQyz/ 51 | on: 52 | appveyor_repo_tag: true 53 | -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.FakeItEasy.Test/FakeItEasyIntegrationTest.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2009-2015 Ninject Project Contributors 4 | // Authors: Scott Xu (scott-xu@msn.com) 5 | // 6 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 7 | // you may not use this file except in compliance with one of the Licenses. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // or 12 | // http://www.microsoft.com/opensource/licenses.mspx 13 | // 14 | // Unless required by applicable law or agreed to in writing, software 15 | // distributed under the License is distributed on an "AS IS" BASIS, 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | // See the License for the specific language governing permissions and 18 | // limitations under the License. 19 | // 20 | //------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel.FakeItEasy 23 | { 24 | using global::FakeItEasy; 25 | 26 | /// 27 | /// Tests the FakeItEasy mocking kernel. 28 | /// 29 | public class FakeItEasyIntegrationTest : IntegrationTest 30 | { 31 | /// 32 | /// Creates the kernel. 33 | /// 34 | /// The newly created kernel. 35 | protected override MockingKernel CreateKernel() 36 | { 37 | return new FakeItEasyMockingKernel(); 38 | } 39 | 40 | /// 41 | /// Asserts that do was called. 42 | /// 43 | /// The dummy service. 44 | protected override void AssertDoWasCalled(IDummyService dummyService) 45 | { 46 | A.CallTo(() => dummyService.Do()).MustHaveHappened(); 47 | } 48 | 49 | /// 50 | /// Asserts that the delegate was called. 51 | /// 52 | /// The dummy delegate. 53 | protected override void AssertDelegateWasCalledWithArgument(DummyDelegate dummyDelegate) 54 | { 55 | A.CallTo(() => dummyDelegate("argument")).MustHaveHappened(); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.FakeItEasy.Test/Ninject.MockingKernel.FakeItEasy.Test.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp2.0;net452 5 | false 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.FakeItEasy/FakeItEasyMockProvider.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2015-2017 Ninject Project Contributors. All rights reserved. 4 | // 5 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 6 | // You may not use this file except in compliance with one of the Licenses. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // or 11 | // http://www.microsoft.com/opensource/licenses.mspx 12 | // 13 | // Unless required by applicable law or agreed to in writing, software 14 | // distributed under the License is distributed on an "AS IS" BASIS, 15 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | // See the License for the specific language governing permissions and 17 | // limitations under the License. 18 | // 19 | // ------------------------------------------------------------------------------------------------- 20 | 21 | namespace Ninject.MockingKernel.FakeItEasy 22 | { 23 | using System; 24 | using System.Collections.Generic; 25 | using System.Linq; 26 | using System.Reflection; 27 | 28 | using global::FakeItEasy; 29 | using global::FakeItEasy.Creation; 30 | using Ninject.Activation; 31 | using Ninject.Components; 32 | 33 | /// 34 | /// Creates mocked instances via FakeItEasy. 35 | /// 36 | public class FakeItEasyMockProvider : NinjectComponent, IProvider, IMockProviderCallbackProvider 37 | { 38 | /// 39 | /// Additional interfaces the proxy to implement. 40 | /// 41 | private IEnumerable additionalInterfaces; 42 | 43 | /// 44 | /// Gets the type (or prototype) of instances the provider creates. 45 | /// 46 | public Type Type 47 | { 48 | get { return typeof(A); } 49 | } 50 | 51 | /// 52 | /// Gets a callback that creates an instance of the . 53 | /// 54 | /// The created callback. 55 | public Func GetCreationCallback() 56 | { 57 | return ctx => this; 58 | } 59 | 60 | /// 61 | /// Creates an instance within the specified context. 62 | /// 63 | /// The context. 64 | /// The created instance. 65 | public object Create(IContext context) 66 | { 67 | this.additionalInterfaces = context.Parameters.OfType().Select(ai => (Type)ai.GetValue(context, null)); 68 | var fakeMethod = typeof(A).GetMethods(BindingFlags.Public | BindingFlags.Static).Last(m => m.Name == "Fake").MakeGenericMethod(context.Request.Service); 69 | var buildAction = typeof(Action<>).MakeGenericType(typeof(IFakeOptions<>).MakeGenericType(context.Request.Service)); 70 | 71 | var d = Delegate.CreateDelegate(buildAction, this, typeof(FakeItEasyMockProvider).GetMethod("Build", BindingFlags.Instance | BindingFlags.NonPublic).MakeGenericMethod(context.Request.Service)); 72 | 73 | return fakeMethod.Invoke(null, new object[] { d }); 74 | } 75 | 76 | /// 77 | /// Add additional interfaces to the builder. 78 | /// 79 | /// The proxy type. 80 | /// The . 81 | protected virtual void Build(IFakeOptions builder) 82 | { 83 | foreach (var i in this.additionalInterfaces) 84 | { 85 | builder.Implements(i); 86 | } 87 | } 88 | } 89 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.FakeItEasy/FakeItEasyMockingKernel.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2015-2017 Ninject Project Contributors. All rights reserved. 4 | // 5 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 6 | // You may not use this file except in compliance with one of the Licenses. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // or 11 | // http://www.microsoft.com/opensource/licenses.mspx 12 | // 13 | // Unless required by applicable law or agreed to in writing, software 14 | // distributed under the License is distributed on an "AS IS" BASIS, 15 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | // See the License for the specific language governing permissions and 17 | // limitations under the License. 18 | // 19 | // ------------------------------------------------------------------------------------------------- 20 | 21 | namespace Ninject.MockingKernel.FakeItEasy 22 | { 23 | using Modules; 24 | 25 | /// 26 | /// Mocking kernel for NSubstitute 27 | /// 28 | public class FakeItEasyMockingKernel : MockingKernel 29 | { 30 | /// 31 | /// Initializes a new instance of the class. 32 | /// 33 | public FakeItEasyMockingKernel() 34 | { 35 | this.Load(new FakeItEasyModule()); 36 | } 37 | 38 | /// 39 | /// Initializes a new instance of the class. 40 | /// 41 | /// 42 | /// The configuration to use. 43 | /// 44 | /// 45 | /// The modules to load into the kernel. 46 | /// 47 | public FakeItEasyMockingKernel(INinjectSettings settings, params INinjectModule[] modules) 48 | : base(settings, modules) 49 | { 50 | this.Load(new FakeItEasyModule()); 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.FakeItEasy/FakeItEasyModule.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2015-2017 Ninject Project Contributors. All rights reserved. 4 | // 5 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 6 | // You may not use this file except in compliance with one of the Licenses. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // or 11 | // http://www.microsoft.com/opensource/licenses.mspx 12 | // 13 | // Unless required by applicable law or agreed to in writing, software 14 | // distributed under the License is distributed on an "AS IS" BASIS, 15 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | // See the License for the specific language governing permissions and 17 | // limitations under the License. 18 | // 19 | // ------------------------------------------------------------------------------------------------- 20 | 21 | namespace Ninject.MockingKernel.FakeItEasy 22 | { 23 | using Modules; 24 | 25 | /// 26 | /// The module for FakeItEasy. 27 | /// 28 | public class FakeItEasyModule : NinjectModule 29 | { 30 | /// 31 | /// Loads the module into the kernel. 32 | /// 33 | public override void Load() 34 | { 35 | this.Kernel.Components.Add(); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.FakeItEasy/Ninject.MockingKernel.FakeItEasy.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.0;net45 5 | 0.0.0 6 | Ninject Project Contributors 7 | Ninject Project Contributors 8 | Automocking for FakeItEasy 9 | Automock implementation for FakeItEasy using Ninject to create the objects under test. 10 | 2015-2017 Ninject Project Contributors. 11 | true 12 | ..\Ninject.snk 13 | true 14 | true 15 | Ninject.MockingKernel.FakeItEasy 16 | 0.0.0 17 | Ninject;ioc;di;automock;FakeItEasy 18 | http://www.ninject.org/ 19 | https://github.com/ninject/Ninject.MockingKernel/blob/master/CHANGELOG.md 20 | https://github.com/ninject/ninject/raw/master/logos/Ninject-Logo32.png 21 | https://github.com/ninject/Ninject.MockingKernel/raw/master/LICENSE.txt 22 | true 23 | git 24 | https://github.com/ninject/Ninject.MockingKernel 25 | pdbonly 26 | 27 | 28 | 29 | true 30 | true 31 | true 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | All 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.FakeItEasy/stylecop.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", 3 | "settings": { 4 | "documentationRules": { 5 | "companyName": "Ninject Project Contributors", 6 | "copyrightText": " Copyright (c) 2015-2017 Ninject Project Contributors. All rights reserved.\r\n\r\n Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).\r\n You may not use this file except in compliance with one of the Licenses.\r\n You may obtain a copy of the License at\r\n\r\n http://www.apache.org/licenses/LICENSE-2.0\r\n or\r\n http://www.microsoft.com/opensource/licenses.mspx\r\n\r\n Unless required by applicable law or agreed to in writing, software\r\n distributed under the License is distributed on an \"AS IS\" BASIS,\r\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n See the License for the specific language governing permissions and\r\n limitations under the License.", 7 | "headerDecoration": "-------------------------------------------------------------------------------------------------" 8 | }, 9 | "layoutRules": { 10 | "newlineAtEndOfFile": "omit" 11 | }, 12 | "orderingRules": { 13 | "blankLinesBetweenUsingGroups": "require" 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.Moq.Test/DefaultMockRepositoryProviderTest.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2010 bbv Software Services AG 4 | // Author: Remo Gloor remo.gloor@bbv.ch 5 | // 6 | // Licensed under the Apache License, Version 2.0 (the "License"); 7 | // you may not use this file except in compliance with the License. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // 12 | // Unless required by applicable law or agreed to in writing, software 13 | // distributed under the License is distributed on an "AS IS" BASIS, 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | // See the License for the specific language governing permissions and 16 | // limitations under the License. 17 | // 18 | // Also licenced under Microsoft Public License (Ms-PL). 19 | // 20 | //------------------------------------------------------------------------------- 21 | 22 | #if !SILVERLIGHT_30 && !SILVERLIGHT_20 && !NETCF 23 | namespace Ninject.MockingKernel.Moq 24 | { 25 | using FluentAssertions; 26 | using global::Moq; 27 | using Xunit; 28 | 29 | public class DefaultMockRepositoryProviderTest 30 | { 31 | [Fact] 32 | public void CreatesMocksWithDefaultBehaviorByDefault() 33 | { 34 | var testee = new DefaultMockRepositoryProvider { Settings = new NinjectSettings() }; 35 | 36 | var mock = testee.Instance.Create(); 37 | 38 | mock.Behavior.Should().Be(MockBehavior.Default); 39 | mock.CallBase.Should().Be(false); 40 | mock.DefaultValue.Should().Be(DefaultValue.Empty); 41 | } 42 | 43 | [Fact] 44 | public void CreatesMocksWithTheConfiguredMockBehavior() 45 | { 46 | var settings = new NinjectSettings(); 47 | settings.SetMockBehavior(MockBehavior.Strict); 48 | var testee = new DefaultMockRepositoryProvider { Settings = settings }; 49 | 50 | var mock = testee.Instance.Create(); 51 | mock.Behavior.Should().Be(MockBehavior.Strict); 52 | } 53 | 54 | [Fact] 55 | public void CreatesMocksWithTheConfiguredCallBaseBehavior() 56 | { 57 | var settings = new NinjectSettings(); 58 | settings.SetMockCallBase(true); 59 | var testee = new DefaultMockRepositoryProvider { Settings = settings }; 60 | 61 | var mock = testee.Instance.Create(); 62 | mock.CallBase.Should().Be(true); 63 | } 64 | 65 | [Fact] 66 | public void CreatesMocksWithTheConfiguredDefaultReturnValue() 67 | { 68 | var settings = new NinjectSettings(); 69 | settings.SetMockDefaultValue(DefaultValue.Mock); 70 | var testee = new DefaultMockRepositoryProvider { Settings = settings }; 71 | 72 | var mock = testee.Instance.Create(); 73 | mock.DefaultValue.Should().Be(DefaultValue.Mock); 74 | } 75 | } 76 | } 77 | #endif 78 | -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.Moq.Test/MoqIntegrationTest.cs: -------------------------------------------------------------------------------- 1 | // -------------------------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2010 bbv Software Services AG 4 | // Author: Remo Gloor remo.gloor@bbv.ch 5 | // 6 | // Licensed under the Apache License, Version 2.0 (the "License"); 7 | // you may not use this file except in compliance with the License. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // 12 | // Unless required by applicable law or agreed to in writing, software 13 | // distributed under the License is distributed on an "AS IS" BASIS, 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | // See the License for the specific language governing permissions and 16 | // limitations under the License. 17 | // 18 | // Also licenced under Microsoft Public License (Ms-PL). 19 | // 20 | // -------------------------------------------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel.Moq 23 | { 24 | using System; 25 | using System.Reflection; 26 | 27 | using FluentAssertions; 28 | using global::Moq; 29 | 30 | using Ninject.Components; 31 | 32 | using Xunit; 33 | 34 | /// 35 | /// Integration test for the moq mocking kernel. 36 | /// 37 | public class MoqIntegrationTest : IntegrationTest 38 | { 39 | /// 40 | /// Mocks are loose by default 41 | /// 42 | [Fact] 43 | public void MocksAreLooseByDefault() 44 | { 45 | using (var kernel = this.CreateKernel()) 46 | { 47 | var mock = kernel.Get(); 48 | 49 | mock.Do(); 50 | } 51 | } 52 | 53 | /// 54 | /// Mocks are loose by default 55 | /// 56 | [Fact] 57 | public void MocksAreStrictIfConfigured() 58 | { 59 | var settings = new NinjectSettings(); 60 | settings.SetMockBehavior(MockBehavior.Strict); 61 | 62 | using (var kernel = new MoqMockingKernel(settings)) 63 | { 64 | var mock = kernel.Get(); 65 | 66 | Assert.Throws(() => mock.Do()); 67 | } 68 | } 69 | 70 | #if !SILVERLIGHT_30 && !SILVERLIGHT_20 && !NETCF 71 | /// 72 | /// Mocks are loose by default 73 | /// 74 | [Fact] 75 | public void MockRepositoryCanBeAccessed() 76 | { 77 | using (var kernel = new MoqMockingKernel()) 78 | { 79 | kernel.Components.RemoveAll(); 80 | kernel.Components.Add(); 81 | var repository = new MockRepository(MockBehavior.Default); 82 | TestMockRepositoryProvider.Repository = repository; 83 | 84 | kernel.MockRepository.Should().BeSameAs(repository); 85 | } 86 | } 87 | #endif 88 | 89 | /// 90 | /// Creates the kernel. 91 | /// 92 | /// The newly created kernel. 93 | protected override MockingKernel CreateKernel() 94 | { 95 | return new MoqMockingKernel(); 96 | } 97 | 98 | /// 99 | /// Asserts that do was called. 100 | /// 101 | /// The dummy service. 102 | protected override void AssertDoWasCalled(IDummyService dummyService) 103 | { 104 | Mock.Get(dummyService).Verify(service => service.Do()); 105 | } 106 | 107 | /// 108 | /// Asserts that the delegate was called. 109 | /// 110 | /// The dummy delegate. 111 | protected override void AssertDelegateWasCalledWithArgument(DummyDelegate dummyDelegate) 112 | { 113 | Mock.Get(dummyDelegate).Verify(d => d("argument")); 114 | } 115 | 116 | #if !SILVERLIGHT_30 && !SILVERLIGHT_20 && !NETCF 117 | public class TestMockRepositoryProvider : NinjectComponent, IMockRepositoryProvider 118 | { 119 | public static MockRepository Repository { get; set; } 120 | 121 | public MockRepository Instance 122 | { 123 | get 124 | { 125 | return Repository; 126 | } 127 | } 128 | 129 | public MethodInfo CreateMethod 130 | { 131 | get 132 | { 133 | throw new NotImplementedException(); 134 | } 135 | } 136 | 137 | public MethodInfo AddAdditionalInterfaceMethod 138 | { 139 | get 140 | { 141 | throw new NotImplementedException(); 142 | } 143 | } 144 | } 145 | #endif 146 | } 147 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.Moq.Test/Ninject.MockingKernel.Moq.Test.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp2.0;net452 5 | false 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.Moq/DefaultMockRepositoryProvider.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2010 bbv Software Services AG. All rights reserved. 4 | // Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. 5 | // 6 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 7 | // You may not use this file except in compliance with one of the Licenses. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // or 12 | // http://www.microsoft.com/opensource/licenses.mspx 13 | // 14 | // Unless required by applicable law or agreed to in writing, software 15 | // distributed under the License is distributed on an "AS IS" BASIS, 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | // See the License for the specific language governing permissions and 18 | // limitations under the License. 19 | // 20 | // ------------------------------------------------------------------------------------------------- 21 | 22 | #if !SILVERLIGHT_30 && !SILVERLIGHT_20 && !NETCF 23 | namespace Ninject.MockingKernel.Moq 24 | { 25 | using System; 26 | using System.Reflection; 27 | 28 | using global::Moq; 29 | using Ninject.Components; 30 | 31 | /// 32 | /// Providers a MockRepository that is configured as Loose by default. 33 | /// The MockBehavior can be overridden by setting a different one on the NinjectSettings 34 | /// using SetMockBehavior. 35 | /// 36 | public class DefaultMockRepositoryProvider : NinjectComponent, IMockRepositoryProvider 37 | { 38 | /// 39 | /// the instance of the mock repository. 40 | /// 41 | private MockRepository instance; 42 | 43 | /// 44 | /// Gets the method info of the create method. 45 | /// 46 | private MethodInfo createMethod; 47 | 48 | /// 49 | /// Gets the method info of the add additional interface method. 50 | /// 51 | private MethodInfo addAdditionalInterfaceMethod; 52 | 53 | /// 54 | /// Gets the instance of the mock repository. 55 | /// 56 | /// The instance of the mock repository. 57 | public MockRepository Instance 58 | { 59 | get 60 | { 61 | if (this.instance == null) 62 | { 63 | this.instance = new MockRepository(this.Settings.GetMockBehavior()) 64 | { 65 | CallBase = this.Settings.GetMockCallBase(), 66 | DefaultValue = this.Settings.GetMockDefaultValue(), 67 | }; 68 | } 69 | 70 | return this.instance; 71 | } 72 | } 73 | 74 | /// 75 | /// Gets the method info of the create method. 76 | /// 77 | /// The method info of the create method. 78 | public MethodInfo CreateMethod 79 | { 80 | get 81 | { 82 | if (this.createMethod == null) 83 | { 84 | this.createMethod = this.Instance.GetType().GetMethod("Create", new Type[0]); 85 | } 86 | 87 | return this.createMethod; 88 | } 89 | } 90 | 91 | /// 92 | /// Gets the method info of the add additional interface method. 93 | /// 94 | /// The method info of the add additional interface method. 95 | public MethodInfo AddAdditionalInterfaceMethod 96 | { 97 | get 98 | { 99 | if (this.addAdditionalInterfaceMethod == null) 100 | { 101 | this.addAdditionalInterfaceMethod = typeof(Mock).GetMethod("As"); 102 | } 103 | 104 | return this.addAdditionalInterfaceMethod; 105 | } 106 | } 107 | } 108 | } 109 | #endif -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.Moq/IMockRepositoryProvider.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2010 bbv Software Services AG. All rights reserved. 4 | // Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. 5 | // 6 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 7 | // You may not use this file except in compliance with one of the Licenses. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // or 12 | // http://www.microsoft.com/opensource/licenses.mspx 13 | // 14 | // Unless required by applicable law or agreed to in writing, software 15 | // distributed under the License is distributed on an "AS IS" BASIS, 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | // See the License for the specific language governing permissions and 18 | // limitations under the License. 19 | // 20 | // ------------------------------------------------------------------------------------------------- 21 | 22 | #if !SILVERLIGHT_30 && !SILVERLIGHT_20 && !NETCF 23 | namespace Ninject.MockingKernel.Moq 24 | { 25 | using System.Reflection; 26 | 27 | using global::Moq; 28 | using Ninject.Components; 29 | 30 | /// 31 | /// Provider for the MockRepository used to create new mock instances. 32 | /// 33 | public interface IMockRepositoryProvider : INinjectComponent 34 | { 35 | /// 36 | /// Gets the instance. 37 | /// 38 | /// The instance. 39 | MockRepository Instance { get; } 40 | 41 | /// 42 | /// Gets the method info of the create method. 43 | /// 44 | /// The method info of the create method. 45 | MethodInfo CreateMethod { get; } 46 | 47 | /// 48 | /// Gets the method info of the add additional interface method. 49 | /// 50 | /// the method info of the add additional interface method. 51 | MethodInfo AddAdditionalInterfaceMethod { get; } 52 | } 53 | } 54 | #endif -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.Moq/MoqMockProvider.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2010 bbv Software Services AG. All rights reserved. 4 | // Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. 5 | // 6 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 7 | // You may not use this file except in compliance with one of the Licenses. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // or 12 | // http://www.microsoft.com/opensource/licenses.mspx 13 | // 14 | // Unless required by applicable law or agreed to in writing, software 15 | // distributed under the License is distributed on an "AS IS" BASIS, 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | // See the License for the specific language governing permissions and 18 | // limitations under the License. 19 | // 20 | // ------------------------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel.Moq 23 | { 24 | using System; 25 | using System.Linq; 26 | using System.Reflection; 27 | 28 | using global::Moq; 29 | using Ninject.Activation; 30 | using Ninject.Components; 31 | using Ninject.MockingKernel; 32 | 33 | /// 34 | /// Creates mocked instances via Moq. 35 | /// 36 | public class MoqMockProvider : NinjectComponent, IProvider, IMockProviderCallbackProvider 37 | { 38 | #if !SILVERLIGHT_30 && !SILVERLIGHT_20 && !NETCF 39 | /// 40 | /// The mock repository provider used to create mock instances. 41 | /// 42 | private readonly MockRepository mockRepository; 43 | 44 | /// 45 | /// The method info used to create mock instances. 46 | /// 47 | private readonly MethodInfo createMethod; 48 | 49 | /// 50 | /// The method info used to add additional interface to mock. 51 | /// 52 | private readonly MethodInfo addAdditionalInterfaceMethod; 53 | 54 | /// 55 | /// Initializes a new instance of the class. 56 | /// 57 | /// The mock repository provider used to create mock instances. 58 | public MoqMockProvider(IMockRepositoryProvider mockRepositoryProvider) 59 | { 60 | this.mockRepository = mockRepositoryProvider.Instance; 61 | this.createMethod = mockRepositoryProvider.CreateMethod; 62 | this.addAdditionalInterfaceMethod = mockRepositoryProvider.AddAdditionalInterfaceMethod; 63 | } 64 | #endif 65 | 66 | /// 67 | /// Gets the type (or prototype) of instances the provider creates. 68 | /// 69 | public Type Type 70 | { 71 | get 72 | { 73 | return typeof(Mock<>); 74 | } 75 | } 76 | 77 | /// 78 | /// Gets a callback that creates an instance of the . 79 | /// 80 | /// The created callback. 81 | public Func GetCreationCallback() 82 | { 83 | return ctx => this; 84 | } 85 | 86 | #if !SILVERLIGHT_30 && !SILVERLIGHT_20 && !NETCF 87 | /// 88 | /// Creates an instance within the specified context. 89 | /// 90 | /// The context. 91 | /// The created instance. 92 | [System.Security.SecuritySafeCritical] 93 | public object Create(IContext context) 94 | { 95 | var methodInfo = this.createMethod.MakeGenericMethod(context.Request.Service); 96 | var mock = (Mock)methodInfo.Invoke(this.mockRepository, new object[0]); 97 | var additionalInterfaces = context.Parameters.OfType().Select(ai => (Type)ai.GetValue(context, null)); 98 | foreach (var additionalInterface in additionalInterfaces) 99 | { 100 | this.addAdditionalInterfaceMethod.MakeGenericMethod(additionalInterface).Invoke(mock, null); 101 | } 102 | 103 | return mock.Object; 104 | } 105 | 106 | #else 107 | /// 108 | /// Creates an instance within the specified context. 109 | /// 110 | /// The context. 111 | /// The created instance. 112 | #if !NETCF 113 | [System.Security.SecuritySafeCritical] 114 | #endif 115 | public object Create(IContext context) 116 | { 117 | var mockType = typeof(Mock<>).MakeGenericType(context.Request.Service); 118 | var constructorInfo = mockType.GetConstructor(new[] { typeof(MockBehavior) }); 119 | var mock = (Mock)constructorInfo.Invoke(new object[] { Settings.GetMockBehavior() }); 120 | var additionalInterfaces = context.Parameters.OfType().Select(ai => (Type)ai.GetValue(context, null)); 121 | foreach (var additionalInterface in additionalInterfaces) 122 | { 123 | typeof(Mock).GetMethod("As").MakeGenericMethod(additionalInterface).Invoke(mock, null); 124 | } 125 | 126 | return mock.Object; 127 | } 128 | #endif 129 | } 130 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.Moq/MoqMockingKernel.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2010 bbv Software Services AG. All rights reserved. 4 | // Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. 5 | // 6 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 7 | // You may not use this file except in compliance with one of the Licenses. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // or 12 | // http://www.microsoft.com/opensource/licenses.mspx 13 | // 14 | // Unless required by applicable law or agreed to in writing, software 15 | // distributed under the License is distributed on an "AS IS" BASIS, 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | // See the License for the specific language governing permissions and 18 | // limitations under the License. 19 | // 20 | // ------------------------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel.Moq 23 | { 24 | using global::Moq; 25 | using Ninject.Modules; 26 | 27 | /// 28 | /// Mocking kernel for moq 29 | /// 30 | public class MoqMockingKernel : MockingKernel 31 | { 32 | /// 33 | /// Initializes a new instance of the class. 34 | /// 35 | public MoqMockingKernel() 36 | { 37 | this.Load(new MoqModule()); 38 | } 39 | 40 | /// 41 | /// Initializes a new instance of the class. 42 | /// 43 | /// The configuration to use. 44 | /// The modules to load into the kernel. 45 | public MoqMockingKernel(INinjectSettings settings, params INinjectModule[] modules) 46 | : base(settings, modules) 47 | { 48 | this.Load(new MoqModule()); 49 | } 50 | 51 | #if !SILVERLIGHT_30 && !SILVERLIGHT_20 && !NETCF 52 | /// 53 | /// Gets the mock repository. 54 | /// 55 | /// The mock repository. 56 | public MockRepository MockRepository 57 | { 58 | get 59 | { 60 | return this.Components.Get().Instance; 61 | } 62 | } 63 | #endif 64 | 65 | /// 66 | /// Gets the mock. 67 | /// 68 | /// The type of the mock to be returned. 69 | /// The mock for the given type. 70 | public Mock GetMock() 71 | where T : class 72 | { 73 | return Mock.Get(this.Get()); 74 | } 75 | } 76 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.Moq/MoqModule.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2010 bbv Software Services AG. All rights reserved. 4 | // Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. 5 | // 6 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 7 | // You may not use this file except in compliance with one of the Licenses. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // or 12 | // http://www.microsoft.com/opensource/licenses.mspx 13 | // 14 | // Unless required by applicable law or agreed to in writing, software 15 | // distributed under the License is distributed on an "AS IS" BASIS, 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | // See the License for the specific language governing permissions and 18 | // limitations under the License. 19 | // 20 | // ------------------------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel.Moq 23 | { 24 | using Ninject.Modules; 25 | 26 | /// 27 | /// The module for moq 28 | /// 29 | public class MoqModule : NinjectModule 30 | { 31 | /// 32 | /// Loads the module into the kernel. 33 | /// 34 | public override void Load() 35 | { 36 | this.Kernel.Components.Add(); 37 | #if !SILVERLIGHT_30 && !SILVERLIGHT_20 && !NETCF 38 | this.Kernel.Components.Add(); 39 | #endif 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.Moq/Ninject.MockingKernel.Moq.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.0;net45 5 | 0.0.0 6 | Ninject Project Contributors 7 | Ninject Project Contributors 8 | Automocking for Moq 9 | Automock implementation for Moq using Ninject to create the objects under test. 10 | 2010 bbv Software Services AG. 2010-2017 Ninject Project Contributors. 11 | true 12 | ..\Ninject.snk 13 | true 14 | true 15 | Ninject.MockingKernel.Moq 16 | 0.0.0 17 | Ninject;ioc;di;automock;Moq 18 | http://www.ninject.org/ 19 | https://github.com/ninject/Ninject.MockingKernel/blob/master/CHANGELOG.md 20 | https://github.com/ninject/ninject/raw/master/logos/Ninject-Logo32.png 21 | https://github.com/ninject/Ninject.MockingKernel/raw/master/LICENSE.txt 22 | true 23 | git 24 | https://github.com/ninject/Ninject.MockingKernel 25 | pdbonly 26 | 27 | 28 | 29 | true 30 | true 31 | true 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | All 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.Moq/NinjectSettingsExtensions.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2010 bbv Software Services AG. All rights reserved. 4 | // Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. 5 | // 6 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 7 | // You may not use this file except in compliance with one of the Licenses. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // or 12 | // http://www.microsoft.com/opensource/licenses.mspx 13 | // 14 | // Unless required by applicable law or agreed to in writing, software 15 | // distributed under the License is distributed on an "AS IS" BASIS, 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | // See the License for the specific language governing permissions and 18 | // limitations under the License. 19 | // 20 | // ------------------------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel.Moq 23 | { 24 | using global::Moq; 25 | 26 | /// 27 | /// Extends the ninject settings with a getter and setter method for the default mock behavior. 28 | /// 29 | public static class NinjectSettingsExtensions 30 | { 31 | /// 32 | /// The key used to store the mock behavior in the ninject settings. 33 | /// 34 | private const string MockBehaviorSettingsKey = "MockBehavior"; 35 | 36 | /// 37 | /// The key used to store the mock call base behavior in the ninject settings. 38 | /// 39 | private const string MockCallBase = "MockCallBase"; 40 | 41 | /// 42 | /// The key used to store the mock default return value in the ninject settings. 43 | /// 44 | private const string MockDefaultValue = "MockDefaultValue"; 45 | 46 | /// 47 | /// Sets the mock behavior. 48 | /// 49 | /// The ninject settings. 50 | /// The mock behavior. 51 | public static void SetMockBehavior(this INinjectSettings settings, MockBehavior mockBehavior) 52 | { 53 | settings.Set(MockBehaviorSettingsKey, mockBehavior); 54 | } 55 | 56 | /// 57 | /// Gets the mock behavior. 58 | /// 59 | /// The ninject settings. 60 | /// The configured mock behavior. 61 | public static MockBehavior GetMockBehavior(this INinjectSettings settings) 62 | { 63 | return settings.Get(MockBehaviorSettingsKey, MockBehavior.Default); 64 | } 65 | 66 | /// 67 | /// Sets the mock call base behavior. 68 | /// 69 | /// The ninject settings. 70 | /// The mock call base behavior. 71 | public static void SetMockCallBase(this INinjectSettings settings, bool mockCallBase) 72 | { 73 | settings.Set(MockCallBase, mockCallBase); 74 | } 75 | 76 | /// 77 | /// Gets the mock call base behavior. 78 | /// 79 | /// The ninject settings. 80 | /// The configured mock call base behavior. 81 | public static bool GetMockCallBase(this INinjectSettings settings) 82 | { 83 | return settings.Get(MockCallBase, false); 84 | } 85 | 86 | /// 87 | /// Sets the mock default return value. 88 | /// 89 | /// The ninject settings. 90 | /// The mock default return value. 91 | public static void SetMockDefaultValue(this INinjectSettings settings, DefaultValue mockDefaultValue) 92 | { 93 | settings.Set(MockDefaultValue, mockDefaultValue); 94 | } 95 | 96 | /// 97 | /// Gets the mock default return value. 98 | /// 99 | /// The ninject settings. 100 | /// The configured mock default return value. 101 | public static DefaultValue GetMockDefaultValue(this INinjectSettings settings) 102 | { 103 | return settings.Get(MockDefaultValue, DefaultValue.Empty); 104 | } 105 | } 106 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.Moq/stylecop.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", 3 | "settings": { 4 | "documentationRules": { 5 | "companyName": "Ninject Project Contributors", 6 | "copyrightText": " Copyright (c) 2010 bbv Software Services AG. All rights reserved.\r\n Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved.\r\n\r\n Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).\r\n You may not use this file except in compliance with one of the Licenses.\r\n You may obtain a copy of the License at\r\n\r\n http://www.apache.org/licenses/LICENSE-2.0\r\n or\r\n http://www.microsoft.com/opensource/licenses.mspx\r\n\r\n Unless required by applicable law or agreed to in writing, software\r\n distributed under the License is distributed on an \"AS IS\" BASIS,\r\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n See the License for the specific language governing permissions and\r\n limitations under the License.", 7 | "headerDecoration": "-------------------------------------------------------------------------------------------------" 8 | }, 9 | "layoutRules": { 10 | "newlineAtEndOfFile": "omit" 11 | }, 12 | "orderingRules": { 13 | "blankLinesBetweenUsingGroups": "require" 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.NSubstitute.Test/NSubstituteIntegrationTest.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2011 Andre Loker IT Services 4 | // Author: Andre Loker (mail@loker-it.de) 5 | // 6 | // Licensed under the Apache License, Version 2.0 (the "License"); 7 | // you may not use this file except in compliance with the License. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // 12 | // Unless required by applicable law or agreed to in writing, software 13 | // distributed under the License is distributed on an "AS IS" BASIS, 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | // See the License for the specific language governing permissions and 16 | // limitations under the License. 17 | // 18 | // Also licenced under Microsoft Public License (Ms-PL). 19 | // 20 | //------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel.NSubstitute 23 | { 24 | using global::NSubstitute; 25 | 26 | #if !NO_NSUBSTITUTE_SILVERLIGHT_TESTS 27 | /// 28 | /// Tests the rhino mocks mocking kernel. 29 | /// 30 | public class NSubstituteIntegrationTest : IntegrationTest 31 | { 32 | /// 33 | /// Creates the kernel. 34 | /// 35 | /// The newly created kernel. 36 | protected override MockingKernel CreateKernel() 37 | { 38 | return new NSubstituteMockingKernel(); 39 | } 40 | 41 | /// 42 | /// Asserts that do was called. 43 | /// 44 | /// The dummy service. 45 | protected override void AssertDoWasCalled(IDummyService dummyService) 46 | { 47 | dummyService.Received().Do(); 48 | } 49 | 50 | /// 51 | /// Asserts that the delegate was called. 52 | /// 53 | /// The dummy delegate. 54 | protected override void AssertDelegateWasCalledWithArgument(DummyDelegate dummyDelegate) 55 | { 56 | dummyDelegate.Received()("argument"); 57 | } 58 | } 59 | #endif 60 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.NSubstitute.Test/Ninject.MockingKernel.NSubstitute.Test.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp2.0;net46 5 | false 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.NSubstitute/NSubstituteMockProvider.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2011 Andre Loker IT Services. All rights reserved. 4 | // Copyright (c) 2011-2017 Ninject Project Contributors. All rights reserved. 5 | // 6 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 7 | // You may not use this file except in compliance with one of the Licenses. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // or 12 | // http://www.microsoft.com/opensource/licenses.mspx 13 | // 14 | // Unless required by applicable law or agreed to in writing, software 15 | // distributed under the License is distributed on an "AS IS" BASIS, 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | // See the License for the specific language governing permissions and 18 | // limitations under the License. 19 | // 20 | // ------------------------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel.NSubstitute 23 | { 24 | using System; 25 | using System.Linq; 26 | 27 | using Activation; 28 | using Components; 29 | using global::NSubstitute; 30 | 31 | /// 32 | /// Creates mocked instances via NSubstitute. 33 | /// 34 | public class NSubstituteMockProvider : NinjectComponent, IProvider, IMockProviderCallbackProvider 35 | { 36 | /// 37 | /// Gets the type (or prototype) of instances the provider creates. 38 | /// 39 | public Type Type 40 | { 41 | get { return typeof(Substitute); } 42 | } 43 | 44 | /// 45 | /// Gets a callback that creates an instance of the . 46 | /// 47 | /// 48 | /// The created callback. 49 | /// 50 | public Func GetCreationCallback() 51 | { 52 | return ctx => this; 53 | } 54 | 55 | /// 56 | /// Creates an instance within the specified context. 57 | /// 58 | /// 59 | /// The context. 60 | /// 61 | /// 62 | /// The created instance. 63 | /// 64 | public object Create(IContext context) 65 | { 66 | var additionalInterfaces = context.Parameters.OfType().Select(ai => (Type)ai.GetValue(context, null)); 67 | 68 | return Substitute.For(new[] { context.Request.Service }.Concat(additionalInterfaces).ToArray(), null); 69 | } 70 | } 71 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.NSubstitute/NSubstituteMockingKernel.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2011 Andre Loker IT Services. All rights reserved. 4 | // Copyright (c) 2011-2017 Ninject Project Contributors. All rights reserved. 5 | // 6 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 7 | // You may not use this file except in compliance with one of the Licenses. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // or 12 | // http://www.microsoft.com/opensource/licenses.mspx 13 | // 14 | // Unless required by applicable law or agreed to in writing, software 15 | // distributed under the License is distributed on an "AS IS" BASIS, 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | // See the License for the specific language governing permissions and 18 | // limitations under the License. 19 | // 20 | // ------------------------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel.NSubstitute 23 | { 24 | using Modules; 25 | 26 | /// 27 | /// Mocking kernel for NSubstitute 28 | /// 29 | public class NSubstituteMockingKernel : MockingKernel 30 | { 31 | /// 32 | /// Initializes a new instance of the class. 33 | /// 34 | public NSubstituteMockingKernel() 35 | { 36 | this.Load(new NSubstituteModule()); 37 | } 38 | 39 | /// 40 | /// Initializes a new instance of the class. 41 | /// 42 | /// 43 | /// The configuration to use. 44 | /// 45 | /// 46 | /// The modules to load into the kernel. 47 | /// 48 | public NSubstituteMockingKernel(INinjectSettings settings, params INinjectModule[] modules) 49 | : base(settings, modules) 50 | { 51 | this.Load(new NSubstituteModule()); 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.NSubstitute/NSubstituteModule.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2011 Andre Loker IT Services. All rights reserved. 4 | // Copyright (c) 2011-2017 Ninject Project Contributors. All rights reserved. 5 | // 6 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 7 | // You may not use this file except in compliance with one of the Licenses. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // or 12 | // http://www.microsoft.com/opensource/licenses.mspx 13 | // 14 | // Unless required by applicable law or agreed to in writing, software 15 | // distributed under the License is distributed on an "AS IS" BASIS, 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | // See the License for the specific language governing permissions and 18 | // limitations under the License. 19 | // 20 | // ------------------------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel.NSubstitute 23 | { 24 | using Modules; 25 | 26 | /// 27 | /// The module for moq 28 | /// 29 | public class NSubstituteModule : NinjectModule 30 | { 31 | /// 32 | /// Loads the module into the kernel. 33 | /// 34 | public override void Load() 35 | { 36 | this.Kernel.Components.Add(); 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.NSubstitute/Ninject.MockingKernel.NSubstitute.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.0;net46 5 | 0.0.0 6 | Ninject Project Contributors 7 | Ninject Project Contributors 8 | Automocking for NSubstitute 9 | Automock implementation for NSubstitute using Ninject to create the objects under test. 10 | Copyright (c) 2011 Andre Loker IT Services. 2012-2017 Ninject Project Contributors. 11 | true 12 | ..\Ninject.snk 13 | true 14 | true 15 | Ninject.MockingKernel.NSubstitute 16 | 0.0.0 17 | Ninject;ioc;di;automock;NSubstitute 18 | http://www.ninject.org/ 19 | https://github.com/ninject/Ninject.MockingKernel/blob/master/CHANGELOG.md 20 | https://github.com/ninject/ninject/raw/master/logos/Ninject-Logo32.png 21 | https://github.com/ninject/Ninject.MockingKernel/raw/master/LICENSE.txt 22 | true 23 | git 24 | https://github.com/ninject/Ninject.MockingKernel 25 | pdbonly 26 | 27 | 28 | 29 | true 30 | true 31 | true 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | All 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.NSubstitute/stylecop.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", 3 | "settings": { 4 | "documentationRules": { 5 | "companyName": "Ninject Project Contributors", 6 | "copyrightText": " Copyright (c) 2011 Andre Loker IT Services. All rights reserved.\r\n Copyright (c) 2011-2017 Ninject Project Contributors. All rights reserved.\r\n\r\n Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).\r\n You may not use this file except in compliance with one of the Licenses.\r\n You may obtain a copy of the License at\r\n\r\n http://www.apache.org/licenses/LICENSE-2.0\r\n or\r\n http://www.microsoft.com/opensource/licenses.mspx\r\n\r\n Unless required by applicable law or agreed to in writing, software\r\n distributed under the License is distributed on an \"AS IS\" BASIS,\r\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n See the License for the specific language governing permissions and\r\n limitations under the License.", 7 | "headerDecoration": "-------------------------------------------------------------------------------------------------" 8 | }, 9 | "layoutRules": { 10 | "newlineAtEndOfFile": "omit" 11 | }, 12 | "orderingRules": { 13 | "blankLinesBetweenUsingGroups": "require" 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.Test/IDummyService.cs: -------------------------------------------------------------------------------- 1 | // -------------------------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2010 bbv Software Services AG 4 | // Author: Remo Gloor remo.gloor@bbv.ch 5 | // 6 | // Licensed under the Apache License, Version 2.0 (the "License"); 7 | // you may not use this file except in compliance with the License. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // 12 | // Unless required by applicable law or agreed to in writing, software 13 | // distributed under the License is distributed on an "AS IS" BASIS, 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | // See the License for the specific language governing permissions and 16 | // limitations under the License. 17 | // 18 | // Also licenced under Microsoft Public License (Ms-PL). 19 | // 20 | // -------------------------------------------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel 23 | { 24 | /// 25 | /// A dummy interface 26 | /// 27 | public interface IDummyService 28 | { 29 | /// 30 | /// A dummy method. 31 | /// 32 | void Do(); 33 | } 34 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.Test/IDummyService2.cs: -------------------------------------------------------------------------------- 1 | // -------------------------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2010 bbv Software Services AG 4 | // Author: Remo Gloor remo.gloor@bbv.ch 5 | // 6 | // Licensed under the Apache License, Version 2.0 (the "License"); 7 | // you may not use this file except in compliance with the License. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // 12 | // Unless required by applicable law or agreed to in writing, software 13 | // distributed under the License is distributed on an "AS IS" BASIS, 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | // See the License for the specific language governing permissions and 16 | // limitations under the License. 17 | // 18 | // Also licenced under Microsoft Public License (Ms-PL). 19 | // 20 | // -------------------------------------------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel 23 | { 24 | /// 25 | /// A dummy interface 26 | /// 27 | public interface IDummyService2 28 | { 29 | /// 30 | /// A dummy method. 31 | /// 32 | void Do2(); 33 | } 34 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.Test/IntegrationTest.cs: -------------------------------------------------------------------------------- 1 | // -------------------------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2010 bbv Software Services AG 4 | // Author: Remo Gloor remo.gloor@bbv.ch 5 | // 6 | // Licensed under the Apache License, Version 2.0 (the "License"); 7 | // you may not use this file except in compliance with the License. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // 12 | // Unless required by applicable law or agreed to in writing, software 13 | // distributed under the License is distributed on an "AS IS" BASIS, 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | // See the License for the specific language governing permissions and 16 | // limitations under the License. 17 | // 18 | // Also licenced under Microsoft Public License (Ms-PL). 19 | // 20 | // -------------------------------------------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel 23 | { 24 | using FluentAssertions; 25 | 26 | using Xunit; 27 | 28 | /// 29 | /// Abstract test base for testing mocking kernel implementations 30 | /// 31 | public abstract class IntegrationTest 32 | { 33 | public delegate bool DummyDelegate(string argument); 34 | 35 | /// 36 | /// Mocks are singletons. 37 | /// 38 | [Fact] 39 | public void MocksAreSingletons() 40 | { 41 | using (var kernel = this.CreateKernel()) 42 | { 43 | var firstReference = kernel.Get(); 44 | var secondReference = kernel.Get(); 45 | 46 | firstReference.Should().BeSameAs(secondReference); 47 | } 48 | } 49 | 50 | /// 51 | /// The ToMock extension can be used to configure mocks. 52 | /// 53 | [Fact] 54 | public void ToMockCanBeUsedToConfigureMocks() 55 | { 56 | using (var kernel = this.CreateKernel()) 57 | { 58 | kernel.Bind().ToMock().InTransientScope(); 59 | 60 | var mock1 = kernel.Get(); 61 | var mock2 = kernel.Get(); 62 | 63 | mock1.Should().NotBeSameAs(mock2); 64 | } 65 | } 66 | 67 | /// 68 | /// The ToMock extension can be used to add additional interfaces. 69 | /// 70 | [Fact] 71 | public void ToMockCanBeUsedToAddAdditionalInterfaces() 72 | { 73 | using (var kernel = this.CreateKernel()) 74 | { 75 | kernel.Bind().ToMock(typeof(IDummyService2)); 76 | 77 | var mock = kernel.Get(); 78 | 79 | mock.Should().BeAssignableTo(); 80 | } 81 | } 82 | 83 | /// 84 | /// Reals the objects are created for auto bindable types. 85 | /// 86 | [Fact] 87 | public void RealObjectsAreCreatedForAutoBindableTypes() 88 | { 89 | using (var kernel = this.CreateKernel()) 90 | { 91 | var instance = kernel.Get(); 92 | 93 | instance.Should().NotBeNull(); 94 | } 95 | } 96 | 97 | /// 98 | /// Reals objects are singletons. 99 | /// 100 | [Fact] 101 | public void RealObjectsAreSingletons() 102 | { 103 | using (var kernel = this.CreateKernel()) 104 | { 105 | var instance1 = kernel.Get(); 106 | var instance2 = kernel.Get(); 107 | 108 | instance1.Should().BeSameAs(instance2); 109 | } 110 | } 111 | 112 | /// 113 | /// The injected dependencies are actually mocks. 114 | /// 115 | [Fact] 116 | public void TheInjectedDependenciesAreMocks() 117 | { 118 | using (var kernel = this.CreateKernel()) 119 | { 120 | var instance = kernel.Get(); 121 | instance.DummyService.Do(); 122 | instance.DummyDelegate("argument"); 123 | 124 | this.AssertDoWasCalled(instance.DummyService); 125 | this.AssertDelegateWasCalledWithArgument(instance.DummyDelegate); 126 | } 127 | } 128 | 129 | [Fact] 130 | public void NamedDependencyCanBeAutoMocked() 131 | { 132 | using (var kernel = this.CreateKernel()) 133 | { 134 | var instance = kernel.Get(); 135 | } 136 | } 137 | 138 | /// 139 | /// Asserts that do was called. 140 | /// 141 | /// The dummy service. 142 | protected abstract void AssertDoWasCalled(IDummyService dummyService); 143 | 144 | /// 145 | /// Asserts that the delegate was called. 146 | /// 147 | /// The dummy delegate. 148 | protected abstract void AssertDelegateWasCalledWithArgument(DummyDelegate dummyDelegate); 149 | 150 | /// 151 | /// Creates the kernel. 152 | /// 153 | /// The newly created kernel. 154 | protected abstract MockingKernel CreateKernel(); 155 | 156 | /// 157 | /// A dummy test class. 158 | /// 159 | public class DummyClass 160 | { 161 | /// 162 | /// Initializes a new instance of the class. 163 | /// 164 | /// The dummy service. 165 | /// The dummy delegate 166 | public DummyClass(IDummyService dummyService, DummyDelegate dummyDelegate) 167 | { 168 | this.DummyService = dummyService; 169 | this.DummyDelegate = dummyDelegate; 170 | } 171 | 172 | /// 173 | /// Gets or sets the dummy service. 174 | /// 175 | /// The dummy service. 176 | public IDummyService DummyService { get; set; } 177 | 178 | public DummyDelegate DummyDelegate { get; set; } 179 | } 180 | 181 | public class DummyClassWithNamedDependency 182 | { 183 | public DummyClassWithNamedDependency([Named("Foo")] IDummyService dummyService) 184 | { 185 | } 186 | } 187 | } 188 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel.Test/Ninject.MockingKernel.Test.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp2.0;net452 5 | false 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/Ninject.MockingKernel/AdditionalInterfaceParameter.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2010 bbv Software Services AG. All rights reserved. 4 | // Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. 5 | // 6 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 7 | // You may not use this file except in compliance with one of the Licenses. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // or 12 | // http://www.microsoft.com/opensource/licenses.mspx 13 | // 14 | // Unless required by applicable law or agreed to in writing, software 15 | // distributed under the License is distributed on an "AS IS" BASIS, 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | // See the License for the specific language governing permissions and 18 | // limitations under the License. 19 | // 20 | // ------------------------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel 23 | { 24 | using System; 25 | 26 | using Ninject.Activation; 27 | using Ninject.Parameters; 28 | using Ninject.Planning.Targets; 29 | 30 | /// 31 | /// Additional interface to implement for the mock. 32 | /// 33 | public class AdditionalInterfaceParameter : IParameter 34 | { 35 | /// 36 | /// The type of additional interface. 37 | /// 38 | private Type additionalInterface; 39 | 40 | /// 41 | /// Initializes a new instance of the class. 42 | /// 43 | /// The type of additional interface. 44 | public AdditionalInterfaceParameter(Type additionalInterface) 45 | { 46 | this.additionalInterface = additionalInterface; 47 | } 48 | 49 | /// 50 | /// Gets the name of the parameter. 51 | /// 52 | public string Name 53 | { 54 | get 55 | { 56 | throw new NotImplementedException(); 57 | } 58 | } 59 | 60 | /// 61 | /// Gets a value indicating whether the parameter should be inherited into child requests. 62 | /// 63 | public bool ShouldInherit 64 | { 65 | get 66 | { 67 | return false; 68 | } 69 | } 70 | 71 | /// 72 | /// Get the interface array. 73 | /// 74 | /// The context. 75 | /// The target 76 | /// The interface type. 77 | public object GetValue(IContext context, ITarget target) 78 | { 79 | return this.additionalInterface; 80 | } 81 | 82 | /// 83 | /// Determines whether the object equals the specified object. 84 | /// 85 | /// An object to compare with this object. 86 | /// True if the objects are equal; otherwise false. 87 | public bool Equals(IParameter other) 88 | { 89 | throw new NotImplementedException(); 90 | } 91 | } 92 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel/ExtensionsForBindingSyntax.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2010 bbv Software Services AG. All rights reserved. 4 | // Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. 5 | // 6 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 7 | // You may not use this file except in compliance with one of the Licenses. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // or 12 | // http://www.microsoft.com/opensource/licenses.mspx 13 | // 14 | // Unless required by applicable law or agreed to in writing, software 15 | // distributed under the License is distributed on an "AS IS" BASIS, 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | // See the License for the specific language governing permissions and 18 | // limitations under the License. 19 | // 20 | // ------------------------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel 23 | { 24 | using System; 25 | 26 | using Ninject.Syntax; 27 | 28 | /// 29 | /// Extensions for the fluent binding syntax API. 30 | /// 31 | public static class ExtensionsForBindingSyntax 32 | { 33 | /// 34 | /// Indicates that the service should be bound to a mocked instance of the specified type. 35 | /// 36 | /// The service that is being mocked. 37 | /// The builder that is building the binding. 38 | /// The additional interfaces for the mock. 39 | /// The syntax for adding more information to the binding. 40 | public static IBindingWhenInNamedWithOrOnSyntax ToMock(this IBindingToSyntax builder, params Type[] additionalInterfaces) 41 | { 42 | var result = builder.To(); 43 | 44 | var bindingConfiguration = builder.BindingConfiguration; 45 | 46 | foreach (var additionalInterface in additionalInterfaces) 47 | { 48 | bindingConfiguration.Parameters.Add(new AdditionalInterfaceParameter(additionalInterface)); 49 | } 50 | 51 | bindingConfiguration.ProviderCallback = builder.Kernel.Components.Get().GetCreationCallback(); 52 | 53 | return result; 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel/IMockProviderCallbackProvider.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2010 bbv Software Services AG. All rights reserved. 4 | // Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. 5 | // 6 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 7 | // You may not use this file except in compliance with one of the Licenses. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // or 12 | // http://www.microsoft.com/opensource/licenses.mspx 13 | // 14 | // Unless required by applicable law or agreed to in writing, software 15 | // distributed under the License is distributed on an "AS IS" BASIS, 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | // See the License for the specific language governing permissions and 18 | // limitations under the License. 19 | // 20 | // ------------------------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel 23 | { 24 | using System; 25 | 26 | using Ninject.Activation; 27 | using Ninject.Components; 28 | 29 | /// 30 | /// Provides a callback that creates a provider which creates the mock. 31 | /// 32 | public interface IMockProviderCallbackProvider : INinjectComponent 33 | { 34 | /// 35 | /// Gets a callback that creates an instance of the that creates the mock. 36 | /// 37 | /// The created callback. 38 | Func GetCreationCallback(); 39 | } 40 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel/MockMissingBindingResolver.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2010 bbv Software Services AG. All rights reserved. 4 | // Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. 5 | // 6 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 7 | // You may not use this file except in compliance with one of the Licenses. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // or 12 | // http://www.microsoft.com/opensource/licenses.mspx 13 | // 14 | // Unless required by applicable law or agreed to in writing, software 15 | // distributed under the License is distributed on an "AS IS" BASIS, 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | // See the License for the specific language governing permissions and 18 | // limitations under the License. 19 | // 20 | // ------------------------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel 23 | { 24 | using System; 25 | using System.Collections.Generic; 26 | using System.Linq; 27 | 28 | using Ninject.Activation; 29 | using Ninject.Components; 30 | using Ninject.Infrastructure; 31 | using Ninject.Planning.Bindings; 32 | using Ninject.Planning.Bindings.Resolvers; 33 | 34 | /// 35 | /// Missing binding resolver that creates a mock for every none self bindable type. 36 | /// 37 | public class MockMissingBindingResolver : NinjectComponent, IMissingBindingResolver 38 | { 39 | /// 40 | /// The call back provider for creating the mock provider. 41 | /// 42 | private readonly IMockProviderCallbackProvider mockProviderCallbackProvider; 43 | 44 | /// 45 | /// Initializes a new instance of the class. 46 | /// 47 | /// The mock provider callback provider. 48 | public MockMissingBindingResolver(IMockProviderCallbackProvider mockProviderCallbackProvider) 49 | { 50 | this.mockProviderCallbackProvider = mockProviderCallbackProvider; 51 | } 52 | 53 | /// 54 | /// Returns any bindings from the specified collection that match the specified request. 55 | /// 56 | /// The multimap of all registered bindings. 57 | /// The request in question. 58 | /// The series of matching bindings. 59 | public IEnumerable Resolve(Multimap bindings, IRequest request) 60 | { 61 | var service = request.Service; 62 | IList bindingList = new List(); 63 | if (this.TypeIsInterfaceOrAbstract(service)) 64 | { 65 | var binding = new Binding(service) 66 | { 67 | ProviderCallback = this.mockProviderCallbackProvider.GetCreationCallback(), 68 | ScopeCallback = ctx => StandardScopeCallbacks.Singleton, 69 | IsImplicit = true, 70 | }; 71 | 72 | if (request.Target != null && 73 | request.Target.GetCustomAttributes(typeof(NamedAttribute), false).FirstOrDefault() is NamedAttribute namedAttribute) 74 | { 75 | binding.Metadata.Name = namedAttribute.Name; 76 | } 77 | 78 | bindingList.Add(binding); 79 | } 80 | 81 | return bindingList; 82 | } 83 | 84 | /// 85 | /// Returns a value indicating whether the specified service is self-bindable. 86 | /// 87 | /// The service. 88 | /// if the type is self-bindable; otherwise . 89 | protected virtual bool TypeIsInterfaceOrAbstract(Type service) 90 | { 91 | return service.IsInterface || service.IsAbstract || typeof(MulticastDelegate).IsAssignableFrom(service); 92 | } 93 | } 94 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel/MockingKernel.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2010 bbv Software Services AG. All rights reserved. 4 | // Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. 5 | // 6 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 7 | // You may not use this file except in compliance with one of the Licenses. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // or 12 | // http://www.microsoft.com/opensource/licenses.mspx 13 | // 14 | // Unless required by applicable law or agreed to in writing, software 15 | // distributed under the License is distributed on an "AS IS" BASIS, 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | // See the License for the specific language governing permissions and 18 | // limitations under the License. 19 | // 20 | // ------------------------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel 23 | { 24 | using Ninject.Activation.Caching; 25 | using Ninject.Modules; 26 | using Ninject.Planning.Bindings.Resolvers; 27 | 28 | /// 29 | /// A kernel that will create mocked instances (via Moq) for any service that is 30 | /// requested for which no binding is registered. 31 | /// 32 | public class MockingKernel : StandardKernel 33 | { 34 | /// 35 | /// Initializes a new instance of the class. 36 | /// 37 | public MockingKernel() 38 | { 39 | this.AddComponents(); 40 | } 41 | 42 | /// 43 | /// Initializes a new instance of the class. 44 | /// 45 | /// The modules to load into the kernel. 46 | public MockingKernel(params INinjectModule[] modules) 47 | : base(modules) 48 | { 49 | this.AddComponents(); 50 | } 51 | 52 | /// 53 | /// Initializes a new instance of the class. 54 | /// 55 | /// The configuration to use. 56 | /// The modules to load into the kernel. 57 | public MockingKernel(INinjectSettings settings, params INinjectModule[] modules) 58 | : base(settings, modules) 59 | { 60 | this.AddComponents(); 61 | } 62 | 63 | /// 64 | /// Clears the kernel's cache, immediately deactivating all activated instances regardless of scope. 65 | /// This does not remove any modules, extensions, or bindings. 66 | /// 67 | public void Reset() 68 | { 69 | this.Components.Get().Clear(); 70 | } 71 | 72 | /// 73 | /// Adds components to the kernel during startup. 74 | /// 75 | private new void AddComponents() 76 | { 77 | this.Components.RemoveAll(); 78 | this.Components.Add(); 79 | this.Components.Add(); 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel/Ninject.MockingKernel.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.0;net45 5 | 0.0.0 6 | Ninject Project Contributors 7 | Ninject Project Contributors 8 | Automocking based on Ninject 9 | Automock implementation using Ninject to create the objects under test. 10 | 2010 bbv Software Services AG. 2010-2017 Ninject Project Contributors. 11 | true 12 | ..\Ninject.snk 13 | true 14 | true 15 | Ninject.MockingKernel 16 | 0.0.0 17 | Ninject;ioc;di;automock 18 | http://www.ninject.org/ 19 | https://github.com/ninject/Ninject.MockingKernel/blob/master/CHANGELOG.md 20 | https://github.com/ninject/ninject/raw/master/logos/Ninject-Logo32.png 21 | https://github.com/ninject/Ninject.MockingKernel/raw/master/LICENSE.txt 22 | true 23 | git 24 | https://github.com/ninject/Ninject.MockingKernel 25 | pdbonly 26 | 27 | 28 | 29 | true 30 | true 31 | true 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | All 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /src/Ninject.MockingKernel/SingletonSelfBindingResolver.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------------------------------- 2 | // 3 | // Copyright (c) 2010 bbv Software Services AG. All rights reserved. 4 | // Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved. 5 | // 6 | // Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL). 7 | // You may not use this file except in compliance with one of the Licenses. 8 | // You may obtain a copy of the License at 9 | // 10 | // http://www.apache.org/licenses/LICENSE-2.0 11 | // or 12 | // http://www.microsoft.com/opensource/licenses.mspx 13 | // 14 | // Unless required by applicable law or agreed to in writing, software 15 | // distributed under the License is distributed on an "AS IS" BASIS, 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 | // See the License for the specific language governing permissions and 18 | // limitations under the License. 19 | // 20 | // ------------------------------------------------------------------------------------------------- 21 | 22 | namespace Ninject.MockingKernel 23 | { 24 | using System; 25 | using System.Collections.Generic; 26 | 27 | using Ninject.Activation; 28 | using Ninject.Components; 29 | using Ninject.Infrastructure; 30 | using Ninject.Planning.Bindings; 31 | using Ninject.Planning.Bindings.Resolvers; 32 | 33 | /// 34 | /// Resolver for self bindable types. Binds them as singleton to self. 35 | /// 36 | public class SingletonSelfBindingResolver : NinjectComponent, IMissingBindingResolver 37 | { 38 | /// 39 | /// The SelfBindingResolver that is used to create bindings for self bindable types. 40 | /// 41 | private readonly SelfBindingResolver selfBindingResolver = new SelfBindingResolver(); 42 | 43 | /// 44 | /// Returns any bindings from the specified collection that match the specified request. 45 | /// 46 | /// The multimap of all registered bindings. 47 | /// The request in question. 48 | /// The series of matching bindings. 49 | public IEnumerable Resolve(Multimap bindings, IRequest request) 50 | { 51 | var newBindings = this.selfBindingResolver.Resolve(bindings, request); 52 | foreach (var binding in newBindings) 53 | { 54 | binding.ScopeCallback = StandardScopeCallbacks.Singleton; 55 | } 56 | 57 | return newBindings; 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /src/Ninject.MockingKernel/stylecop.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", 3 | "settings": { 4 | "documentationRules": { 5 | "companyName": "Ninject Project Contributors", 6 | "copyrightText": " Copyright (c) 2010 bbv Software Services AG. All rights reserved.\r\n Copyright (c) 2010-2017 Ninject Project Contributors. All rights reserved.\r\n\r\n Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).\r\n You may not use this file except in compliance with one of the Licenses.\r\n You may obtain a copy of the License at\r\n\r\n http://www.apache.org/licenses/LICENSE-2.0\r\n or\r\n http://www.microsoft.com/opensource/licenses.mspx\r\n\r\n Unless required by applicable law or agreed to in writing, software\r\n distributed under the License is distributed on an \"AS IS\" BASIS,\r\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n See the License for the specific language governing permissions and\r\n limitations under the License.", 7 | "headerDecoration": "-------------------------------------------------------------------------------------------------" 8 | }, 9 | "layoutRules": { 10 | "newlineAtEndOfFile": "omit" 11 | }, 12 | "orderingRules": { 13 | "blankLinesBetweenUsingGroups": "require" 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /src/Ninject.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ninject/Ninject.MockingKernel/fcf2292e3b24ba2cf9ef0502ca1a21c5897cbe2c/src/Ninject.snk --------------------------------------------------------------------------------