Created
January 2, 2019 10:29
-
-
Save Kralizek/c9381496056710ef6a95bc99cbafdfdb to your computer and use it in GitHub Desktop.
AutoMoq can't generate a mock for a delegate when in the signature of a test method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<TargetFramework>netcoreapp2.0</TargetFramework> | |
<IsPackable>false</IsPackable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="AutoFixture" Version="4.6.0" /> | |
<PackageReference Include="AutoFixture.AutoMoq" Version="4.6.0" /> | |
<PackageReference Include="AutoFixture.NUnit3" Version="4.6.0" /> | |
<PackageReference Include="Moq" Version="4.10.1" /> | |
<PackageReference Include="nunit" Version="3.11.0" /> | |
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" /> | |
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using AutoFixture; | |
using AutoFixture.AutoMoq; | |
using AutoFixture.NUnit3; | |
using Moq; | |
using NUnit.Framework; | |
namespace Tests | |
{ | |
public class Tests | |
{ | |
[Test, AutoMoqData] | |
public void WontWork(Func<string> func, string result) | |
{ | |
Assert.That(func, Is.Not.Null); | |
Mock.Get(func).Setup(p => p()).Returns(result); | |
var value = func(); | |
Assert.That(value, Is.EqualTo(result)); | |
} | |
[Test, AutoMoqData] | |
public void WillWork(string result) | |
{ | |
Func<string> func = Mock.Of<Func<string>>(); | |
Assert.That(func, Is.Not.Null); | |
Mock.Get(func).Setup(p => p()).Returns(result); | |
var value = func(); | |
Assert.That(value, Is.EqualTo(result)); | |
} | |
} | |
public class AutoMoqDataAttribute : AutoDataAttribute | |
{ | |
public AutoMoqDataAttribute() : base(CreateFixture) { } | |
private static IFixture CreateFixture() | |
{ | |
var fixture = new Fixture(); | |
fixture.Customize(new AutoMoqCustomization | |
{ | |
ConfigureMembers = true, | |
GenerateDelegates = true | |
}); | |
return fixture; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment