Skip to content

Commit f2bb328

Browse files
authored
Merge pull request #7319 from vritant24/dev/vrbhardw/fakes
Update Documentation with .NET Core support
2 parents fb2a60c + ecade76 commit f2bb328

4 files changed

+87
-21
lines changed

docs/test/code-generation-compilation-and-naming-conventions-in-microsoft-fakes.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ This article discusses options and issues in Fakes code generation and compilati
1616

1717
- Visual Studio Enterprise
1818
- A .NET Framework project
19-
20-
> [!NOTE]
21-
> .NET Standard projects are not supported.
19+
::: moniker range=">=vs-2019"
20+
- .NET Core and SDK-style project support previewed in Visual Studio 2019 Update 6, and is enabled by default in Update 8. For more information, see [Microsoft Fakes for .NET Core and SDK-style projects](/visualstudio/releases/2019/release-notes#microsoft-fakes-for-net-core-and-sdk-style-projects).
21+
::: moniker-end
2222

2323
## Code generation and compilation
2424

docs/test/isolating-code-under-test-with-microsoft-fakes.md

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ Fakes come in two flavors:
2727

2828
- Visual Studio Enterprise
2929
- A .NET Framework project
30-
- .NET Core and SDK-style project support is currently in preview. [Read More](/visualstudio/releases/2019/release-notes#microsoft-fakes-for-net-core-and-sdk-style-projects)
30+
::: moniker range=">=vs-2019"
31+
- .NET Core and SDK-style project support previewed in Visual Studio 2019 Update 6, and is enabled by default in Update 8. For more information, see [Microsoft Fakes for .NET Core and SDK-style projects](/visualstudio/releases/2019/release-notes#microsoft-fakes-for-net-core-and-sdk-style-projects).
32+
::: moniker-end
3133

3234
> [!NOTE]
33-
> - .NET Standard projects are not supported.
3435
> - Profiling with Visual Studio is not available for tests that use Microsoft Fakes.
3536
3637
## Choose between stub and shim types
@@ -76,11 +77,15 @@ For a more detailed description, see [Use stubs to isolate parts of your applica
7677

7778
2. **Add Fakes Assembly**
7879

79-
1. In **Solution Explorer**, expand the test project's reference list. If you are working in Visual Basic, you must choose **Show All Files** in order to see the reference list.
80+
1. In **Solution Explorer**,
81+
- For an older .NET Framework Project (non-SDK style), expand your unit test project's **References** node.
82+
::: moniker range=">=vs-2019"
83+
- For an SDK-style project targeting .NET Framework or .NET Core, expand the **Dependencies** node to find the assembly you would like to fake under **Assemblies**, **Projects**, or **Packages**.
84+
::: moniker-end
85+
- If you're working in Visual Basic, select **Show All Files** in the **Solution Explorer** toolbar to see the **References** node.
86+
2. Select the assembly that contains the class definitions for which you want to create shims. For example, if you want to shim **DateTime**, select **System.dll**.
8087

81-
2. Select the reference to the assembly in which the interface (for example IStockFeed) is defined. On the shortcut menu of this reference, choose **Add Fakes Assembly**.
82-
83-
3. Rebuild the solution.
88+
3. On the shortcut menu, choose **Add Fakes Assembly**.
8489

8590
3. In your tests, construct instances of the stub and provide code for its methods:
8691

@@ -239,6 +244,61 @@ System.IO.Fakes.ShimFile.AllInstances.ReadToEnd = ...
239244

240245
You can also create shims for specific instances, for constructors, and for properties. For more information, see [Use shims to isolate your application from other assemblies for unit testing](../test/using-shims-to-isolate-your-application-from-other-assemblies-for-unit-testing.md).
241246

247+
## Using Microsoft Fakes in the CI
248+
249+
### Microsoft Fakes Assembly Generation
250+
Since Microsoft Fakes requires Visual Studio Enterprise, the generation of Fakes Assemblies requires that you build your project using [Visual Studio Build Task](/azure/devops/pipelines/tasks/build/visual-studio-build?view=azure-devops).
251+
252+
::: moniker range=">=vs-2019"
253+
> [!NOTE]
254+
> An alternative to this is to check your Fakes Assemblies into the CI and use the [MSBuild Task](../msbuild/msbuild-task.md?view=vs-2019). When you do this, you need to ensure that you have an assembly reference to the generated Fakes assembly in your test project, similar to the following code snippet :
255+
256+
```xml
257+
<Project Sdk="Microsoft.NET.Sdk">
258+
<ItemGroup>
259+
<Reference Include="FakesAssemblies\System.Fakes.dll">
260+
</ItemGroup>
261+
</Project>
262+
```
263+
264+
This reference is required to be added in manually specifically SDK-style projects (.NET Core and .NET Framework) because we have moved to implicitly adding assembly references to your test project. If you follow this method, you need to ensure that the fakes assembly is updated when the parent assembly changes.
265+
::: moniker-end
266+
267+
### Running Microsoft Fakes tests
268+
As long as Microsoft Fakes assemblies are present in the configured `FakesAssemblies` directory (The default being `$(ProjectDir)FakesAssemblies`), you can run tests using the [vstest task](/azure/devops/pipelines/tasks/test/vstest?view=azure-devops).
269+
270+
::: moniker range=">=vs-2019"
271+
Distributed testing with the [vstest task](/azure/devops/pipelines/tasks/test/vstest?view=azure-devops) .NET Core projects using Microsoft Fakes requires Visual Studio 2019 Update 9 Preview `20201020-06` and higher.
272+
::: moniker-end
273+
274+
::: moniker range=">=vs-2019"
275+
## Transitioning your .NET Framework test projects that use Microsoft Fakes to SDK-style .NET Framework or .NET Core projects
276+
You will need minimal changes in your .NET Framework set up for Microsoft Fakes to transition to .NET Core. The cases that you would have to consider are:
277+
- If you are using a custom project template, you need to ensure that it is SDK-style and builds for a compatible target framework.
278+
- Certain types exist in different assemblies in .NET Framework and .NET Core (for example, `System.DateTime` exists in `System`/`mscorlib` in .NET Framework, and in `System.Runtime` in .NET Core), and in these scenarios you need to change the assembly being faked.
279+
- If you have an assembly reference to a fakes assembly and the test project, you might see a build warning about a missing reference similar to:
280+
```
281+
(ResolveAssemblyReferences target) ->
282+
warning MSB3245: Could not resolve this reference. Could not locate the assembly "AssemblyName.Fakes". Check to make sure the assembly exists on disk.
283+
If this reference is required by your code, you may get compilation errors.
284+
```
285+
This warning is due to necessary changes made in Fakes generation can be ignored. It can be avoided by removing the assembly reference from the project file, because we now implicitly add them during the build.
286+
::: moniker-end
287+
288+
## Microsoft Fakes support
289+
### Microsoft Fakes in older projects targeting .NET Framework (non-SDK style).
290+
- Microsoft Fakes assembly generation is supported in Visual Studio Enterprise 2015 and higher.
291+
- Microsoft Fakes tests can run with all available Microsoft.TestPlatform NuGet packages.
292+
- Code coverage is supported for test projects using Microsoft Fakes in Visual Studio Enterprise 2015 and higher.
293+
294+
### Microsoft Fakes in SDK-style .NET Framework and .NET Core projects
295+
- Microsoft Fakes assembly generation previewed in Visual Studio Enterprise 2019 Update 6 and is enabled by default in Update 8.
296+
- Microsoft Fakes tests for projects that target .NET Framework can run with all available Microsoft.TestPlatform NuGet packages.
297+
- Microsoft Fakes tests for projects that target .NET Core can run with Microsoft.TestPlatform NuGet packages with versions [16.8.0-preview-20200921-01](https://www.nuget.org/packages/Microsoft.TestPlatform/16.8.0-preview-20200921-01) and higher.
298+
- Code coverage is supported for test projects targeting .NET Framework using Microsoft Fakes in Visual Studio Enterprise version 2015 and higher.
299+
- Code coverage support for test projects targeting .NET Core using Microsoft Fakes is under development.
300+
301+
242302
## In this section
243303
[Use stubs to isolate parts of your application from each other for unit testing](../test/using-stubs-to-isolate-parts-of-your-application-from-each-other-for-unit-testing.md)
244304

docs/test/using-shims-to-isolate-your-application-from-other-assemblies-for-unit-testing.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ For an overview and "quick start" guidance, see [Isolate code under test with Mi
2222

2323
- Visual Studio Enterprise
2424
- A .NET Framework project
25-
26-
> [!NOTE]
27-
> .NET Standard projects are not supported.
25+
::: moniker range=">=vs-2019"
26+
- .NET Core and SDK-style project support previewed in Visual Studio 2019 Update 6, and is enabled by default in Update 8. For more information, see [Microsoft Fakes for .NET Core and SDK-style projects](/visualstudio/releases/2019/release-notes#microsoft-fakes-for-net-core-and-sdk-style-projects).
27+
::: moniker-end
2828

2929
## Example: The Y2K bug
3030

@@ -61,9 +61,12 @@ using (ShimsContext.Create()) {
6161

6262
First, add a Fakes assembly:
6363

64-
1. In **Solution Explorer**, expand your unit test project's **References** node.
65-
66-
- If you're working in Visual Basic, select **Show All Files** in the **Solution Explorer** toolbar in order to see the **References** node.
64+
1. In **Solution Explorer**,
65+
- For an older .NET Framework Project (non-SDK style), expand your unit test project's **References** node.
66+
::: moniker range=">=vs-2019"
67+
- For an SDK-style project targeting .NET Framework or .NET Core, expand the **Dependencies** node to find the assembly you would like to fake under **Assemblies**, **Projects**, or **Packages**.
68+
::: moniker-end
69+
- If you're working in Visual Basic, select **Show All Files** in the **Solution Explorer** toolbar to see the **References** node.
6770

6871
2. Select the assembly that contains the class definitions for which you want to create shims. For example, if you want to shim **DateTime**, select **System.dll**.
6972

@@ -514,7 +517,7 @@ System.Fakes.ShimEnvironment.GetCommandLineArgsGet = ...
514517

515518
## Limitations
516519

517-
Shims cannot be used on all types from the .NET base class library **mscorlib** and **System**.
520+
Shims cannot be used on all types from the .NET base class library **mscorlib** and **System** in .NET Framework, and in **System.Runtime** in .NET Core.
518521

519522
## See also
520523

docs/test/using-stubs-to-isolate-parts-of-your-application-from-each-other-for-unit-testing.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,14 @@ To use stubs, you must first generate stub types from the interface definitions.
141141

142142
#### Add a Fakes Assembly
143143

144-
1. In **Solution Explorer**, expand your unit test project's **References**.
145-
146-
If you're working in Visual Basic, select **Show All Files** in the **Solution Explorer** toolbar in order to see the **References** node.
147-
148-
2. Select the assembly that contains the interface definitions for which you want to create stubs.
144+
1. In **Solution Explorer**,
145+
- For an older .NET Framework Project (non-SDK style), expand your unit test project's **References** node.
146+
::: moniker range=">=vs-2019"
147+
- For an SDK-style project targeting .NET Framework or .NET Core, expand the **Dependencies** node to find the assembly you would like to fake under **Assemblies**, **Projects**, or **Packages**.
148+
::: moniker-end
149+
- If you're working in Visual Basic, select **Show All Files** in the **Solution Explorer** toolbar to see the **References** node.
150+
151+
2. Select the assembly that contains the class definitions for which you want to create shims. For example, if you want to shim **DateTime**, select **System.dll**.
149152

150153
3. On the shortcut menu, choose **Add Fakes Assembly**.
151154

0 commit comments

Comments
 (0)