当具体对象是依赖项时,如何告诉 PEX 使用模拟对象,然后自动生成测试用例?
我正在提供的框架中编写客户端组件,并且需要能够对我的组件进行单元测试。这些组件是使用 MVP(模型-视图-演示者)模式编写的,我想使用 PEX 为我的演示者自动生成单元测试。
以下是演示者的代码。
public partial class CompetitorPresenter : PresenterBase
{
private readonly ICompetitorView _view;
public IGlobalDataAccess GlobalDataAccess;
public IGlobalUI Globals;
public SystemClient Client;
public bool DeleteRecord()
{
if (_view.CompetitorName != "Daniel")
return false;
if (Client.SystemName != "Ruby")
return false;
return true;
}
}
我遇到的问题是对象 SystemClient
是由框架提供的,我无法使用工厂类来创建 SystemClient
的实例。因此,当我运行 PEX 自动生成单元测试时,我必须告诉 PEX 忽略 SystemClient
,这样做的结果是方法 DeleteRecord
没有完全覆盖,如行Client.SystemName != "Ruby"
未经测试。
由于我有模拟对象MSystemClient
(使用moles创建),我想知道在配置中的某个位置我是否可以告诉PEX使用MSystemClient
,并让PEX自动生成测试例来完全涵盖此方法。
I am writing client-side components in a provided framework, and need to be able to unit test my components. The components are written using MVP (Model-View-Presenter) pattern, I want to use PEX to automatically generate unit tests for my presenters.
The following is the code of a presenter.
public partial class CompetitorPresenter : PresenterBase
{
private readonly ICompetitorView _view;
public IGlobalDataAccess GlobalDataAccess;
public IGlobalUI Globals;
public SystemClient Client;
public bool DeleteRecord()
{
if (_view.CompetitorName != "Daniel")
return false;
if (Client.SystemName != "Ruby")
return false;
return true;
}
}
The problem I am having is that the object SystemClient
is provided by the framework, and I cannot use a factory class to create an instance of SystemClient
. Therefore when I run PEX to automatically generate unit tests, I have to tell PEX to ignore SystemClient
, the result of this is that the method DeleteRecord
is not fully covered as the line Client.SystemName != "Ruby"
is not tested.
Since I have the mock object MSystemClient
(created using moles), I am wondering if somewhere in the configuration I could tell PEX to use MSystemClient
, and let PEX to automatically generate test cases to fully cover this method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你走在正确的轨道上。如果您无法控制
CompetitorPresenter.Client
实例的创建位置,您可以为所有实例定义一个摩尔:您的单元测试必须在“托管环境”中运行:
You are on the right track. If you cannot control where the instance of
CompetitorPresenter.Client
is created, you can define a mole for all instances:Your unit test has to be run in a "hosted environment":