我试图对私有方法进行简单的单元测试,我让 VS2010 自动生成方法存根并添加缺少的信息:这是我拥有的代码
[TestMethod()]
[DeploymentItem("MyPackage.Language.dll")]
public void getValidCultureWithValidInputCulture()
{
CultureInfo culture = new CultureInfo("sv-SE", false);
PrivateObject param0 = new PrivateObject(culture, new PrivateType(typeof(CultureInfo)));
GlobalResourceProvider_Accessor target = new GlobalResourceProvider_Accessor(param0);
CultureInfo expected = new CultureInfo("sv-SE", false);
CultureInfo actual = target.getValidCulture(culture);
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}
当我运行它时,我得到一个异常:
System.ArgumentException:
The member specified (getValidCulture) could not be found. You might need to regenerate your private accessor,
or the member may be private and defined on a base class. If the latter is true, you need to pass the type
that defines the member into PrivateObject's constructor.
有谁知道我在这里做错了什么?我的目标是学习如何使用 PrivateType 和 PrivateObject 进行私有方法测试。
编辑:
我不想使用 PrivateObject.Invoke (它对重构不友好)。