2

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.

4

1 回答 1

0

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

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

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

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

  // Test code...
}
于 2012-01-06T09:48:25.153 回答