Skip to content

Instantly share code, notes, and snippets.

@Kralizek
Created January 2, 2019 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kralizek/c9381496056710ef6a95bc99cbafdfdb to your computer and use it in GitHub Desktop.
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
<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>
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