【发布时间】:2014-05-19 08:25:59
【问题描述】:
我正在为以下类遗留类编写单元测试
Class myLegacyClassPresenter
{
private MethodA(){}
private propertyA {get; set;}
private MethodB(YearValue value){}
//some more properties & method goes here
private struct YearValue
{
public static int One { get { return 365; } }
public static int Two { get { return 730; } }
}
}
这是我的单元测试。
public void mytest()
{
//some initializations
var view = myLegacyView();
var service = new Mock<ILegacyService>();
var presenter = new myLegacyClassPresenter(view, service);
var privateObject = new PrivateObject(presenter);
//I can access all private methods and properties as follows
privateObject.invoke("MethodA");
privateObject.GetProperty("propertyA")
// But How can I get the the Struct Year value to pass to MethodB
privateObject.Invoke("MethodB", new object[]{YearValue.One}); //Compile Error for YearValue
//I can't change in the class, One way is to define the same struct locally, Is there any other approach we can have to achieve the same result.
}
【问题讨论】:
-
YearValue是MethodB的参数吗?如果是这样,无论您的单元测试如何,您的代码都不会编译(这是错误CS0050)。 -
@PatrickQuirk 感谢您指出这一点,刚刚更新。
-
我同意@RyanGates,但目前我只是在更新旧的单元测试,它使用的是访问私有方法
myLegacyClassPresenter_PrivateAccessor' toPrivateObject`的折旧方法` -
这可能是一个错字,但 MethodB 采用 YearValue 但您试图通过 int (YearValue.One) 调用它。我在下面有一个答案,显示了如何传递 YearValue。如果需要传入 int YearValue.One 可以使用反射从答案中获取的类型中获取值
标签: c# unit-testing moq vs-unit-testing-framework