How does Moq verify Generic Func Parameter? - c#

Given
public interface IService
{
void Run<T>(T value, Func<T, string> func);
}
public class Sut
{
private readonly IService m_service;
public Sut(IService service)
{
m_service = service;
}
public void Test()
{
Point point = new Point {X = 1, Y = 2};
m_service.Run(point, p => $"X:{p.X}, Y:{p.Y}");
}
}
internal class Point
{
public int X { get; set; }
public int Y { get; set; }
}
And tested like
[Test]
public void RunTest()
{
// Arrange
Mock<IService> mockService = new Mock<IService>();
var sut = new Sut(mockService.Object);
// Act
sut.Test();
// Assert
mockService.Verify(e => e.Run(It.IsAny<It.IsAnyType>(), It.IsAny<Func<It.IsAnyType, string>>()), Times.Once);
}
This test will failed, due to Point is third party internal class, I can not use It.IsAny<Point> in the verify.
if I change the Point to the public and verify use following, it's working good.
mockService.Verify(e => e.Run(It.IsAny<Point>(), It.IsAny<Func<Point, string>>()), Times.Once);
any help would be appreciated

Generally, you want to put an InternalsVisibleTo attribute in the assembly under test, pointing to the test assembly.
See here for reference information.

Related

C# Create mock of delegate static method for nUnit test

I am working on nUnit. I need to Setup mock of a static method from same class that is under subject. I am wonder If I created mock correctly as I have defined two instances; sut of class under test and mock of same class method in objective to MockObject.Setup(..) that exist in the same class that I am trying to test. I need help to create mock of GetNumbers() method that is in ProcessCalculation class
error
main
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("Testing Automation Proptype... ");
ProcessCalculation process = new ProcessCalculation();
process.Run();
Console.ReadLine();
}
}
Calculation
public class ProcessCalculation
{
public delegate List<int> GetNumbersPointer();
//GetNumbersPointer getNumbersPointer = new GetNumbersPointer(GetNumbers);
public GetNumbersPointer getNumbersPointer = GetNumbers;
public void Run()
{
var Nos = getNumbersPointer.Invoke();
int x = Nos[0];
int y = Nos[1];
var z = Add(x, y);
Console.WriteLine("sum "+ z);
}
public static int Add(int x, int y)
{
return x + y;
}
public static List<int> GetNumbers()
{
List<int> myNo = new List<int>();
myNo.Add(32);
myNo.Add(10);
return myNo;
}
}
Test Class
public class Tests
{
private readonly ProcessCalculation sut;
private readonly Mock<ProcessCalculation> processCalculationMoq;
public Tests()
{
sut = new ProcessCalculation();
processCalculationMoq = new Mock<ProcessCalculation>();
}
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
//Arrange
var fixture = new Fixture();
var getNumberMoq = fixture.CreateMany<int>(2);
processCalculationMoq.Setup(x => x.getNumbersPointer).Returns(getNumberMoq); // this throw error.. I want to mock using setup
//???????????? how use delegate to mock static GetNumbers() method
//Assert
}
}
Change your implementation to
public class ProcessCalculation
{
public delegate List<int> GetNumbersPointer();
// If InternalsVisibleTo(Unit Test assembly) is specified, this can be internal
// Also, "new DelegateType(Target)" is not needed, the compiler inserts
// that implicitly
public GetNumbersPointer getNumbersPointer = GetNumbers;
// ...
}
And then, in your test:
public class Tests
{
private readonly ProcessCalculation sut;
public Tests()
{
sut = new ProcessCalculation();
sut.getNumbersPointer = TestGetNumbers;
}
[Test]
public void Test1()
{
sut.Run();
}
public static List<int> TestGetNumbers()
{
// Return test data
return new List<int> { 0, 1, 2, 3...};
}
}

Mock a method of class under test with Moq & AutoMock

I've been reading around and I can't seem to find a case that matches the behavior I want with Moq.
I want to mock a specific method (which is interfacing with an external API) of a class that I want to test. The problem is that, as I understand it, once the class is .Create<T>, you cannot .Setup()... any method on it.
Here is an example:
public class ClassA
{
public void ConfigureExpansion()
{
var classB = new ClassB();
var data = GetExternalData(); // I want to mock this
//data is used in the code
classB.TaskB(data); // I want to mock this
}
public string GetExternalData()
{
return data...
}
}
public class ClassB
{
public void TaskB(String myData)
{
//Does some work
}
}
[TestMethod]
public void Test()
{
using (var mock = AutoMock.GetLoose())
{
mock.Mock<IClassB>().Setup(x => x.TaskB(It.IsAny<string>()));
var cls = mock.Create<ClassA>();
cls.Setup(x => x.GetExternalData(It.IsAny<string>())).Return("MyData"); // <--- This is not valid, but represents what I am trying to do.
//act
cls.ConfigureExpansion();
mock.Mock<IClassB>().Verify(x => x.TaskB(), Times.Once);
}
}
I tried to set up a mock for the interface of my class under test, but the cls won't call it:
[TestMethod]
public void Test()
{
using (var mock = AutoMock.GetLoose())
{
mock.Mock<IClassA>().Setup(x => x.GetExternalData(It.IsAny<string>())).Return("MyData"); //<--- Mocking the method here is not detected by the cls below
mock.Mock<IClassB>().Setup(x => x.TaskB(It.IsAny<string>()));
var cls = mock.Create<ClassA>();
//act
cls.ConfigureExpansion();
mock.Mock<IClassB>().Verify(x => x.TaskB(), Times.Once);
}
}
I've also tried instantiating my class-under-test and when I do it like this, it doesn't take the mocks (ClassB) into consideration:
[TestMethod]
public void Test()
{
using (var mock = AutoMock.GetLoose())
{
mock.Mock<IClassA>().Setup(x => x.GetExternalData(It.IsAny<string>())).Return("MyData"); //<--- Mocking the method here is not detected by the cls below
mock.Mock<IClassB>().Setup(x => x.TaskB(It.IsAny<string>()));
var cls = new ClassA();
//act
cls.ConfigureExpansion();
mock.Mock<IClassB>().Verify(x => x.TaskB(), Times.Once);
}
}
I would really appreciate any insights about this,
Thank you
Here is one approach for your problem. If you want to test ConfigureExpansion of ClassA, you have to mock the dependencies used in that method, which is ClassB. You cannot mock GetExternalData of ClassA because ClassA is the class under test now. You may have to refactor your classes in a way so that there is a ClassUnderTest which creates both instances of ClassA and ClassB using dependency injection. Then, you can use Moq to mock the interfaces IClassA and IClassB to test the ConfigureExpansion.
public class ClassUnderTest
{
private readonly IClassA _classA;
private readonly IClassB _classB;
public ClassUnderTest(IClassA classA, IClassB classB)
{
this._classA = classA;
this._classB = classB;
}
public void ConfigureExpansion()
{
var data = this._classA.GetExternalData();
this._classB.TaskB(data);
}
}
public interface IClassA
{
string GetExternalData();
}
public class ClassA : IClassA
{
public string GetExternalData()
{
return "some data";
}
}
public interface IClassB
{
void TaskB(string myData);
}
public class ClassB : IClassB
{
public void TaskB(string myData)
{
//Does some work
}
}
The test method can be:
[TestMethod]
public void Test()
{
using (var mock = AutoMock.GetLoose())
{
var mockClassA = mock.Mock<IClassA>();
var mockClassB = mock.Mock<IClassB>();
mockClassA.Setup(x => x.GetExternalData()).Returns("MyData");
var cls = new ClassUnderTest(mockClassA.Object, mockClassB.Object);
//act
cls.ConfigureExpansion();
mockClassB.Verify(x => x.TaskB(It.IsAny<string>()), Times.Once);
}
}

Partial mock methods in a class using Autofixture, Moq and XUnit

I want to mock only some methods of a class and call the real implementation for other methods.
I have my sut class Test where the Runner class is injected in the constructor. This injected class has again a injected other class RunnerParam in the constructor.
The code is a simplified case of my real classes in trying to have only the basics.
[Fact]
public void Test()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var paramMock = fixture.Freeze<Mock<IRunnerParam>>();
paramMock.Setup(x => x.Multiplicator()).Returns(2);
var classMock = fixture.Freeze<Mock<IRunner>>();
classMock.Setup(x => x.Run()).Returns(5);
var test = fixture.Create<Test>();
var result = test.StartRunning(); // should be 5
var result2 = test.StartRunningImplementation(5); // should be 500
}
Supporting members
public interface IRunnerParam
{
int Multiplicator();
}
public class RunnerParam : IRunnerParam
{
public virtual int Multiplicator()
{
return 20;
}
}
public interface IRunner
{
int Run();
int RunImplementation(int param);
}
public class Runner : IRunner
{
protected virtual RunnerParam MultiParam { get; set; }
public Runner(RunnerParam multiParam)
{
MultiParam = multiParam;
}
public virtual int Run()
{
return 10;
}
public int RunImplementation(int param)
{
return 10 * MultiParam.Multiplicator() * param * Run();
}
}
public class Test
{
private readonly IRunner _runner;
public Test(IRunner runner)
{
_runner = runner;
}
public int StartRunning()
{
return _runner.Run();
}
public int StartRunningImplementation(int param)
{
return _runner.RunImplementation(param);
}
}
I want to mock and give a mocked value to the method Run in the class Runner, but to use the real implementation of the method RunImplementation.
I would expect to see for result2 500, but it's 0, meaning that the method is not seen as mocked up. In my eyes that is correct, but the Moq callbase is equal to true, so the real implementation should be taken, but it isn't.
What am I missing here?
In the shown simplified example, Test is only dependent on IRunner
private readonly IRunner _runner;
public Test(IRunner runner)
{
_runner = runner;
}
So that is all that needs to be mocked if the intention was to test Test class in isolation.
//...
var classMock = fixture.Freeze<Mock<IRunner>>();
classMock.Setup(x => x.Run()).Returns(5);
classMock.Setup(x => x.RunImplementation(It.IsAny<int>())).Returns(500);
//...
If Runner class is to be also tested in isolation, then a mocked RunnerParam would be needed to satisfy its dependencies.
It should however be dependent on the abstraction (interface) and not the concretion (implementation).
protected virtual IRunnerParam MultiParam { get; set; }
public Runner(IRunnerParam multiParam) {
MultiParam = multiParam;
}
This simplifies the isolated test as described in the original question
I want to mock and give a mocked value to the method Run in the class Runner, but to use the real implementation of the method RunImplementation.
//Arrange
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var runnerParam = fixture.Freeze<Mock<IRunnerParam>>()
.Setup(_ => _.Multiplicator())
.Returns(2);
var subjectMock = fixture.Freeze<Mock<Runner>>();
subjectMock.CallBase = true;
subjectMock.Setup(_ => _.Run()).Returns(5);
int expected = 500;
Runner sut = subjectMock.Object;
//Act
var actual = sut.RunImplementation(5); // should be 500
//Assert
actual.Should().Be(expected);

DonorManagementTests does not have a default constructor (nunit/moq)

I'm using moq with nunit and my test doesn't give me a fail or pass. It says it doesn't have a default constructor. I suspect I am not doing something correctly with injecting my interface into the constructor.
DonorManagementTests
[TestFixture]
public class DonorManagementTests
{
private readonly Mock<IValidation> _mockValidation;
private readonly DonorManagement _donorManagement;
public DonorManagementTests(IValidation validation)
{
_mockValidation = new Mock<IValidation>();
_donorManagement = new DonorManagement(_mockValidation.Object);
}
[Test, Description("View correct gift aid to two decimal places")]
public void DonorViewGiftAid()
{
const int donation = 20;
_mockValidation.Setup(x => x.ValidateDonation(donation)).Returns(20.00m);
var res = _donorManagement.GiftAidAmount(donation);
Assert.IsInstanceOf(typeof (decimal), res);
_mockValidation.Verify(x => x.ValidateDonation(donation), Times.Once);
}
}
DonorManagement
public class DonorManagement : IDonor
{
private readonly IValidation _validation;
public DonorManagement(IValidation validation)
{
_validation = validation;
}
public virtual decimal GiftAidAmount(decimal donationAmount)
{
const decimal gaRatio = 17.5m / (100 - 17.5m);
return _validation.ValidateDonation(donationAmount) * gaRatio;
}
}
Any ideas what I need to change in my code?
your test class must have a default C'tor.
change your test class to:
[TestFixture]
public class DonorManagementTests
{
private Mock<IValidation> _mockValidation;
private DonorManagement _donorManagement;
[SetUp]
public TestInit()
{
_mockValidation = new Mock<IValidation>();
_donorManagement = new DonorManagement(_mockValidation.Object);
}
[Test, Description("View correct gift aid to two decimal places")]
public void DonorViewGiftAid()
{
const int donation = 20;
_mockValidation.Setup(x => x.ValidateDonation(donation)).Returns(20.00m);
var res = _donorManagement.GiftAidAmount(donation);
Assert.IsInstanceOf(typeof (decimal), res);
_mockValidation.Verify(x => x.ValidateDonation(donation), Times.Once);
}
}
now each test will be isolated and you'll be able to execute your tests.
NUnit documentation (http://www.nunit.org/index.php?p=testFixture&r=2.5) says:
A non-parameterized fixture must have a default constructor.
A parameterized fixture must have a constructor that matches the parameters provided.

Moq Verify with anonymous type parameter

I have the following test, with supporting classes, but I can't figure out how to verify the call on the dependency.
[TestFixture]
public class AnonymousGenericTypeParameterTests
{
[Test]
public void Test()
{
// Arrange
var dependency = new Mock<IDependency>();
var towns = new List<Town>
{
new Town { Name = "Lifford", County = "Donegal", Country="Ireland", Population = 1658 },
new Town { Name = "Ballyshannon", County = "Donegal", Country="Ireland", Population = 2504 },
new Town { Name = "Buxton", County = "Derbyshire", Country="United Kingdom", Population = 13599 },
};
var sut = new MyClass(dependency.Object);
// Act
sut.DoSomething(towns);
// Assert
// The following line needs to be fixed.
dependency.Verify(d => d.Execute(It.IsAny<IEnumerable<object>>(), It.IsAny<Func<object, decimal?>>()));
}
}
public interface IDependency
{
void Execute<T>(IEnumerable<T> collection, Func<T, decimal?> rateSelector);
}
public class MyClass
{
private readonly IDependency dependency;
public MyClass(IDependency dependency)
{
this.dependency = dependency;
}
public void DoSomething(IEnumerable<Town> towns)
{
var counties = towns.GroupBy(t => new {t.Country,t.County});
foreach (var county in counties)
{
dependency.Execute(county, c => c.Population);
}
}
}
public class Town
{
public string Name { get; set; }
public string County { get; set; }
public int Population { get; set; }
public string Country { get; set; }
}
According to Moq's test output, the performed invocations are:
Dependency.Execute(System.Linq.Lookup`2+Grouping[<>f__AnonymousType0`2[System.String,System.String],UniFocus.Staffscope4.Tests.Town], System.Func`2[UniFocus.Staffscope4.Tests.Town,System.Nullable`1[System.Decimal]])
I see plenty of questions regarding anonymous parameters in Moq (such as this and this and this), but can't find anything relating to using an anonymous type as the actual type parameter.
What can be put in the Verify line so that it actually verifies the call inside?
Note: My example IDependency doesn't return a value (it's already complex enough, I think), but there will be bonus kudos for answers that implictly or explicitly address Setup() as well as Verify().
Update
Jesse's solution only passes the test because I made a bad choice when crafting my example. I should have realised that any IGrouping<out TKey, out TElement> is also an IEnumerable<TElement>. Is there a more universal solution?
Update 2
I feel like my original example was possibly too elaborate and didn't represent well the actual title of my question. Is there any solution that works for this more straightforward and to-the-point example?
using Moq;
using NUnit.Framework;
namespace Tests
{
[TestFixture]
public class SimpleAnonymousGenericTypeParameterTests
{
[Test]
public void Test()
{
// Arrange
var dependency = new Mock<IDependency>();
var sut = new MyClass(dependency.Object);
// Act
sut.DoSomething("Donegal", "Lifford");
// Assert
// This verify works for both calls to Execute()
dependency.Verify(d => d.Execute(It.IsAny<object>()), Times.Exactly(2));
// This verify should specifically refer to only the first call to Execute()
dependency.Verify(d => d.Execute(It.IsAny</*HowToRepresentAnonymousTypeHere*/object>()), Times.Once);
}
public interface IDependency
{
void Execute<T>(T thing);
}
public class MyClass
{
private readonly IDependency dependency;
public MyClass(IDependency dependency)
{
this.dependency = dependency;
}
public void DoSomething(string county, string town)
{
dependency.Execute(new { county, town });
object someUnknownObject = "";
dependency.Execute(someUnknownObject);
}
}
}
}
The accepted answer doesn't work for me, I believe it's because the tests and the object in question are in a different assembly so Moq doesn't know how to reconcile the types and does not match them.
Instead, I created the following helper methods that can verify that the anonymous type provided has the correct fields and values:
public static class AnonHelpers
{
public static object MatchAnonymousType(object expected)
{
return Match.Create(Matcher(expected));
}
private static Predicate<object> Matcher(object expected)
{
return actual =>
{
var expectedProp = expected.GetType().GetProperties().ToDictionary(x => x.Name, x => x.GetValue(expected));
var actualProp = actual.GetType().GetProperties().ToDictionary(x => x.Name, x => x.GetValue(actual));
foreach (var prop in expectedProp)
{
if (!actualProp.ContainsKey(prop.Key))
return false;
if (!prop.Value.Equals(actualProp[prop.Key]))
return false;
}
return true;
};
}
}
They can be used like so:
var anon = new { SomeKey = "some value", SomeOtherKey = 123 };
myMock.Setup(x => x.MyMethod(personIDs, AnonHelpers.MatchAnonymousType(anon))).Verifiable();
This will create a matcher that will use reflection to match the anonymous type based on it's keys and values and then you can use normal verification to see when it's been called.
Since the types are known in the context of the test, you could provide the specific type arguments to the Verify call. The following change got the test to pass:
dependency.Verify(d =>
d.Execute(It.IsAny<IEnumerable<Town>>(), It.IsAny<Func<Town, decimal?>>()));
The same should also work for setups.
With regards to the example in Update 2, the following passes, but it requires knowledge of the inner workings of the DoSomething() method and as far as I know it's the only way to make it work:
var anonymousType = new {county = "Donegal", town = "Lifford"};
dependency.Verify(d => d.Execute(anonymousType), Times.Once);

Categories