简体   繁体   English

Visual Studio 2012 - 单元测试错误

[英]Visual Studio 2012 - Unit Test Error

I am using Visual Studio 2012 and I'm having the following error :"Error 'hw02.World' does not contain a constructor that takes 2 arguments. I'm trying to create unit test for my class: 我正在使用Visual Studio 2012,我遇到以下错误:“错误'hw02.World'不包含带有2个参数的构造函数。我正在尝试为我的类创建单元测试:

class World : IWorld
{

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

The test which creates the error: 创建错误的测试:

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

Thanks for any help 谢谢你的帮助

World is a Internal class (does not have a Public keyword at the class level), if your unit test isn't in the same assembly and if there is no InternalsVisibleTo relationship between these assemblies, your unit test will not see any public constructor and thus complain that there is no constructor with two arguments (from the perspective of the Test assembly this is true). World是一个Internal类(在类级别没有Public关键字),如果您的单元测试不在同一个程序集中,并且这些程序集之间没有InternalsVisibleTo关系,那么您的单元测试将看不到任何公共构造函数和因此抱怨没有带有两个参数的构造函数(从Test程序集的角度看这是真的)。

See the documentation on Default Visibility . 请参阅有关默认可见性的文档。

Either make the class World a public class World or add an InternalsVisibleTo attribute to the assembly that contains the World class. class World public class World设为public class World或将InternalsVisibleTo属性添加到包含World类的程序集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM