如何以 windows 形式 class 对方法进行单元测试?
How to unit test methods in windows form class?
我正在开发一个 c# windows 表单应用程序,并且我有一个存在于主表单中的方法 class。
将方法 A 想象成主窗体的一部分 class。
public void methodA() {
A.someMethod();
B.someMethod();
// some more code
if (someCondition) {
// execute some code
}
// initialize timer and set event handler for timer
// run new thread
}
class A {
someMethod() {...}
}
class B {
someMethod() {...}
}
我将如何运行测试来测试这个methodA(isCondition)的分支逻辑?因为它涉及初始化计时器和 运行ning 线程。我可以在做系统测试时只验证逻辑吗?我不认为模拟定时器和线程函数是可能的。
谢谢!
当然你可以模拟定时器。这是通过创建一个新接口,比如 ITimerWrapper
并使用具体的定时器 class 实现它。基本上是定时器 class 的包装器。然后使用它代替您拥有的具体计时器 class。
曲调为:
public partial class Form1 : Form
{
private readonly ITimerWrapper _timerWrapper;
public Form1(ITimerWrapper timerWrapper)
{
InitializeComponent();
this._timerWrapper = timerWrapper; // of course this is done via dependency injection
this._timerWrapper.Interval = 1000;
}
private void Form1_Load(object sender, EventArgs e)
{
// now you can mock this interface
this._timerWrapper.AddTickHandler(this.Tick_Event);
this._timerWrapper.Start();
}
private void Tick_Event(object sender, EventArgs e)
{
Console.WriteLine("tick tock");
}
}
public interface ITimerWrapper
{
void AddTickHandler(EventHandler eventHandler);
void Start();
void Stop();
int Interval { get; set; }
}
public class TimerWrapper : ITimerWrapper
{
private readonly Timer _timer;
public TimerWrapper()
{
this._timer = new Timer();
}
public int Interval
{
get
{
return this._timer.Interval;
}
set
{
this._timer.Interval = value;
}
}
public void AddTickHandler(EventHandler eventHandler)
{
this._timer.Tick += eventHandler;
}
public void Start()
{
this._timer.Start();
}
public void Stop()
{
this._timer.Stop();
}
}
然后对于新线程的旋转,也可以通过做同样的事情来测试。
底线是有一个接口来分离关注点并在单元测试中模拟接口。
我正在开发一个 c# windows 表单应用程序,并且我有一个存在于主表单中的方法 class。
将方法 A 想象成主窗体的一部分 class。
public void methodA() {
A.someMethod();
B.someMethod();
// some more code
if (someCondition) {
// execute some code
}
// initialize timer and set event handler for timer
// run new thread
}
class A {
someMethod() {...}
}
class B {
someMethod() {...}
}
我将如何运行测试来测试这个methodA(isCondition)的分支逻辑?因为它涉及初始化计时器和 运行ning 线程。我可以在做系统测试时只验证逻辑吗?我不认为模拟定时器和线程函数是可能的。
谢谢!
当然你可以模拟定时器。这是通过创建一个新接口,比如 ITimerWrapper
并使用具体的定时器 class 实现它。基本上是定时器 class 的包装器。然后使用它代替您拥有的具体计时器 class。
曲调为:
public partial class Form1 : Form
{
private readonly ITimerWrapper _timerWrapper;
public Form1(ITimerWrapper timerWrapper)
{
InitializeComponent();
this._timerWrapper = timerWrapper; // of course this is done via dependency injection
this._timerWrapper.Interval = 1000;
}
private void Form1_Load(object sender, EventArgs e)
{
// now you can mock this interface
this._timerWrapper.AddTickHandler(this.Tick_Event);
this._timerWrapper.Start();
}
private void Tick_Event(object sender, EventArgs e)
{
Console.WriteLine("tick tock");
}
}
public interface ITimerWrapper
{
void AddTickHandler(EventHandler eventHandler);
void Start();
void Stop();
int Interval { get; set; }
}
public class TimerWrapper : ITimerWrapper
{
private readonly Timer _timer;
public TimerWrapper()
{
this._timer = new Timer();
}
public int Interval
{
get
{
return this._timer.Interval;
}
set
{
this._timer.Interval = value;
}
}
public void AddTickHandler(EventHandler eventHandler)
{
this._timer.Tick += eventHandler;
}
public void Start()
{
this._timer.Start();
}
public void Stop()
{
this._timer.Stop();
}
}
然后对于新线程的旋转,也可以通过做同样的事情来测试。
底线是有一个接口来分离关注点并在单元测试中模拟接口。