嘲笑源自iList的接口将其传递给应()。

发布于 2025-01-22 11:09:05 字数 1358 浏览 3 评论 0原文

我目前正在测试一种返回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 }
        }
    ); 

但这显然没有编译。

定义 的的汇编具有实现ibuildingzoneibuildingzones的公共类,因此我可以这样使用它们:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慕烟庭风 2025-01-29 11:09:05

您可以使用匿名对象避免使用正在测试的类型。

var expected = new[]
{
   new { Name = "garage", Elevation = 0.0 },
   new { Name = "living_room", Elevation = 10.0 },
   new { Name = "master bedroom", Elevation = -4.2 }
};

building.Zones.Should().BeEquivalentTo(expected);

You can use anonymous objects to avoid using the types under test.

var expected = new[]
{
   new { Name = "garage", Elevation = 0.0 },
   new { Name = "living_room", Elevation = 10.0 },
   new { Name = "master bedroom", Elevation = -4.2 }
};

building.Zones.Should().BeEquivalentTo(expected);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文