【问题标题】:Visual Studio 2012 - Unit Test ErrorVisual Studio 2012 - 单元测试错误
【发布时间】:2014-03-26 03:42:40
【问题描述】:

我正在使用 Visual Studio 2012,但出现以下错误:“错误‘hw02.World’不包含采用 2 个参数的构造函数。我正在尝试为我的类创建单元测试:

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

感谢您的帮助

【问题讨论】:

  • 确定它在看同一个World类吗?你的第一堂课肯定是hw02.World

标签: c# unit-testing visual-studio-2012


【解决方案1】:

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

请参阅Default Visibility 上的文档。

要么将class World 设为public class World,要么将InternalsVisibleTo 属性添加到包含World 类的程序集。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多