嘲笑源自iList的接口将其传递给应()。
我目前正在测试一种返回ibuilding
接口的实例的方法:
public interface IBuilding
{
public string Name { get; set; }
public IBuildingZones Zones { get; set; }
}
public interface IBuildingZones: IList<IBuildingZone>
{
public void SortByElevation();
}
public interface IBuildingZone
{
public string Name { get; set; }
public double Elevation { get; set; }
}
name
值的测试非常容易,但是我正在用最佳方式挣扎测试Zones
以包含一组给定的值。 理想情况下,我会写这篇文章:
building.Zones.Should().BeEquivalentTo(
{
{ "garage", 0 },
{ "living_room", 10 },
{ "master bedroom", -4.2 }
}
);
但这显然没有编译。
定义 的的汇编具有实现
ibuildingzone
和ibuildingzones
的公共类,因此我可以这样使用它们:
var expectedZones = new BuildingZones
{
new BuildingZone("garage", 0),
new BuildingZone("living_room", 10),
new BuildingZone("master_bedroom", -4.2)
};
building.Zones.Should().BeEquivalentTo(expectedZones);
但是我不太高兴要使用正在测试的组件中的类来测试它,尤其是在需要的所有是接口时。
这就是为什么我寻找一种使用MOQ框架嘲笑gucess>预期Zones
的方法,但是我有点困扰着为此找到精益语法。
任何建议是最欢迎的。
I am currently testing a method that returns an instance of the IBuilding
interface defined like this:
public interface IBuilding
{
public string Name { get; set; }
public IBuildingZones Zones { get; set; }
}
public interface IBuildingZones: IList<IBuildingZone>
{
public void SortByElevation();
}
public interface IBuildingZone
{
public string Name { get; set; }
public double Elevation { get; set; }
}
Testing for the Name
value is quite easy, but I'm struggling with the best way to test for the Zones
to contain a given set of values.
Ideally, I would write this:
building.Zones.Should().BeEquivalentTo(
{
{ "garage", 0 },
{ "living_room", 10 },
{ "master bedroom", -4.2 }
}
);
But this obviously does not compile.
The assembly where IBuilding
is defined has public classes that implement IBuildingZone
and IBuildingZones
so I can use them like this:
var expectedZones = new BuildingZones
{
new BuildingZone("garage", 0),
new BuildingZone("living_room", 10),
new BuildingZone("master_bedroom", -4.2)
};
building.Zones.Should().BeEquivalentTo(expectedZones);
but I'm not too happy to use classes from the assembly under test to be able to test it, especially when all that is needed are interfaces.
This is why I went looking for a way to mock expectedZones
using the Moq framework but I'm a bit stuck trying to find a lean syntax for this.
Any suggestion is most welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用匿名对象避免使用正在测试的类型。
You can use anonymous objects to avoid using the types under test.