Visual Studio 2012 - 单元测试错误

4

我正在使用Visual Studio 2012,遇到了以下错误:"Error 'hw02.World' does not contain a constructor that takes 2 arguments." 我想为我的类创建单元测试:

class World : IWorld
{

    public World(int width, int height)
    {
        Width = width;
        Height = height;
        world = new IBuilding[width, height];
    }
}

出现错误的测试:

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        var world = new World(5, 5); // this line creates the error: 'hw02.World' does not contain a constructor that takes 2 arguments
    }
}

感谢您的帮助。

3
你确定它正在查看相同的 World 类吗?你的第一个类肯定是 hw02.World 吗? - Jon Skeet
1个回答

4

World是一个内部类(在类级别没有Public关键字),如果您的单元测试不在同一个程序集中,并且这些程序集之间没有InternalsVisibleTo关系,则您的单元测试将看不到任何公共构造函数,从而抱怨缺少两个参数的构造函数(从测试程序集的角度来看,这是正确的)。

请参阅有关默认可见性的文档。

要么将class World更改为public class World,要么在包含World类的程序集中添加InternalsVisibleTo属性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接