在Visual Studio中创建测试时出错

时间:2009-08-19 11:57:23

标签: visual-studio-2008 unit-testing

当我尝试为以下方法生成单元测试时(在公共静态类中)

private static string[] GetFields(string line, char sep)
{
    char[] totrim = { '"', ' ' };
    return line.Split(sep).Select(col => col.Trim(totrim)).ToArray();
}

测试输出说:

While trying to generate your tests, the following errors occurred:
This method or property cannot be called within an event handler.

如果我将该函数设置为public也可以 - 我尝试手动运行Publicize.exe,它不会抱怨,但也没有任何区别。

1 个答案:

答案 0 :(得分:0)

如果您的功能是私有的,那么它是私有的。没有其他程序集,包括您的测试框架,应该能够直接看到它。

您需要将其公开(这可能会破坏您的模型),公开您明确指出仅用于测试的公共包装函数(可以滥用),或者您可以将其仅用于单元测试组件通过将以下属性(假设您使用C#)添加到包含GetFields

的类
[assembly:InternalsVisibleTo("[your unit test component name]")]

The Microsoft website has more information here