当具体对象是依赖项时,如何告诉 PEX 使用模拟对象,然后自动生成测试用例?

发布于 2024-12-25 00:01:21 字数 953 浏览 0 评论 0原文

我正在提供的框架中编写客户端组件,并且需要能够对我的组件进行单元测试。这些组件是使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北笙凉宸 2025-01-01 00:01:21

你走在正确的轨道上。如果您无法控制 CompetitorPresenter.Client 实例的创建位置,您可以为所有实例定义一个摩尔:

MSystemClient.AllInstances.SystemNameGet = () => "SomeName";

您的单元测试必须在“托管环境”中运行:

[HostType("Moles")]
public void TestMethod()
{
  MSystemClient.AllInstances.SystemNameGet = () => "SomeName";

  // Test code...
}

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:

MSystemClient.AllInstances.SystemNameGet = () => "SomeName";

Your unit test has to be run in a "hosted environment":

[HostType("Moles")]
public void TestMethod()
{
  MSystemClient.AllInstances.SystemNameGet = () => "SomeName";

  // Test code...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文