我正在为以下课程旧版课编写单元测试
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.
}
myLegacyClassPresenter_PrivateAccessor' to
PrivateObject的方法.经典示例显示了如何无法对特定组件进行单元测试,如何对组件进行重新测试!
这是任何模拟框架强制您执行的工作-编写解耦的代码.
几件事情:
应该认真考虑测试私有方法.在测试私有方法和属性时,破坏封装.
在您的示例中,myLegacyClassPresenter
该类与该YearValue
结构紧密结合.您可以使用依赖注入将其解耦.
myLegacyClassPresenter
我不能更改.如果要创建该结构的实例,则可以使用以下内容
var theType = Type.GetType("MyNamespace.myLegacyClassPresenter+YearValue");
var parameter = Activator.CreateInstance(theType);
privateobject.Invoke("MethodB", new object[]{parameter});
如果您需要传递YearValue.One,则可以使用theType.GetMember()获取值.
I am writing Unit tests for the following class legacy class
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; } }
}
}
Here is my unit test.
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
a parameter to MethodB
? If so, your code won't compile regardless of your unit tests (it's error CS0050).myLegacyClassPresenter_PrivateAccessor' to
PrivateObject`Classic example which shows how if you cannot unit test a particular component, REFACTOR the component!
This is where is love what any mocking framework enforces you to do - write decoupled code.
Couple of things:
Testing private methods should be seriously reconsidered. You break encapsulation the moment you test private methods and properties.
In your example, the myLegacyClassPresenter
class is very tightly coupled with the YearValue
struct. You could decouple it using dependency injection.
myLegacyClassPresenter
cannot be changed in my case.If you want to create an instance of the struct you can use something like the following
var theType = Type.GetType("MyNamespace.myLegacyClassPresenter+YearValue");
var parameter = Activator.CreateInstance(theType);
privateobject.Invoke("MethodB", new object[]{parameter});
If you need to pass in YearValue.One, then theType.GetMember() can be used to get the value.
本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文