gpt4 book ai didi

c# - Moq 使用匿名类型参数验证

转载 作者:太空狗 更新时间:2023-10-29 18:22:14 24 4
gpt4 key购买 nike

我有以下测试,带有支持类,但我不知道如何验证对依赖项的调用。

[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; }
}

根据 Moq 的测试输出,执行的调用是:

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]])

我在 Moq 中看到很多关于匿名参数的问题(例如 thisthisthis ),但找不到任何与使用匿名类型作为实际类型参数相关的问题。

可以在 Verify 行中放置什么,以便它实际验证内部调用?

注意:我的例子IDependency不返回值(我认为它已经足够复杂了),但是对于隐式或显式解决 Setup() 的答案将会有额外的奖励。以及Verify() .

更新Jesse 的解决方案只通过了测试,因为我在制作示例时做出了错误的选择。我应该意识到任何 IGrouping<out TKey, out TElement>也是一个 IEnumerable<TElement> .有没有更通用的解决方案?

更新 2我觉得我原来的例子可能过于详尽,不能很好地代表我问题的实际标题。是否有任何解决方案适用于这个更直接、切题的示例?

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);
}
}
}
}

最佳答案

接受的答案对我不起作用,我认为这是因为测试和有问题的对象在不同的​​程序集中,所以 Moq 不知道如何协调类型并且不匹配它们。

相反,我创建了以下辅助方法来验证所提供的匿名类型是否具有正确的字段和值:

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;
};
}
}

它们可以这样使用:

var anon = new { SomeKey = "some value", SomeOtherKey = 123 };
myMock.Setup(x => x.MyMethod(personIDs, AnonHelpers.MatchAnonymousType(anon))).Verifiable();

这将创建一个匹配器,它将使用反射来匹配基于它的键和值的匿名类型,然后您可以使用正常验证来查看它何时被调用。

关于c# - Moq 使用匿名类型参数验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21757421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com