How do you apply extension-based methods?

I have an extension method with the following signature (in the BuildServerExtensions class):

public static IEnumerable<BuildAgent> GetEnabledBuildAgents(
                                          this IBuildServer buildServer,
                                          string teamProjectName)
{
    // omitted agrument validation and irrelevant code
    var buildAgentSpec = buildServer.CreateBuildAgentSpec(teamProjectName);
}

And another method that calls the first (in the BuildAgentSelector class):

public BuildAgent Select(IBuildServer buildServer, string teamProjectName)
{
    // omitted argument validation
    IEnumerable<BuildAgent> serverBuildAgents = 
        buildServer.GetEnabledBuildAgents(teamProjectName);

    // omitted - test doesn't get this far
}

And I'm trying to test it with MSTest and Rhino.Mocks (v3.4) with:

[TestMethod]
public void SelectReturnsNullOnNullBuildAgents()
{
    Mocks = new MockRepository();
    IBuildServer buildServer = Mocks.CreateMock<IBuildServer>();

    BuildAgentSelector buildAgentSelector = new BuildAgentSelector();
    using (Mocks.Record())
    {
        Expect.Call(buildServer.GetEnabledBuildAgents(TeamProjectName)).Return(null);
    }

    using (Mocks.Playback())
    {
        BuildAgent buildAgent = buildAgentSelector.Select(buildServer, TeamProjectName);

        Assert.IsNull(buildAgent);
    }
}

When I run this test, I get:

System.InvalidOperationException:

The previous method IBuildServer.CreateBuildAgentSpec("TeamProjectName");requires a return value or an exception for throw.

This obviously calls the real extension method, not the test implementation. My next desire was to try:

Expect.Call(BuildServerExtensions.GetEnabledBuildAgents(buildServer, TeamProjectName))
      .Return(null);

Then I noticed that my expectations for Rhino.Mocks to intercept this were probably inappropriate.

The question arises: how to eliminate this dependence and make the Select testable method?

, BuildAgentSelector , -, , - , , , .

+3
3

. , . , . .GetEnabledBuildAgents(...)... (, TypeMock Isolator, , ... .)

IBuildAgent, :.CreateBuildAgentSpec(...). , CreateBuildAgentSpec . "", . . IBuildAgent... , , .

:

[TestMethod]
public void SelectReturnsNullOnNullBuildAgents()
{
    Mocks = new MockRepository();
    IBuildServer buildServer = Mocks.CreateMock<IBuildServer>();

    BuildAgent agent = new BuildAgent { ... }; // Create an agent
    BuildAgentSelector buildAgentSelector = new BuildAgentSelector();
    using (Mocks.Record())
    {
        Expect.Call(buildServer.CreateBuildAgentSpec(TeamProjectName)).Return(new List<BuildAgent> { agent });
    }

    using (Mocks.Playback())
    {
        BuildAgent buildAgent = buildAgentSelector.Select(buildServer, TeamProjectName);

        Assert.IsNull(buildAgent);
    }
}

BuildAgent List <BuildAgent> , IEnumerable <BuildAgent> . . , , BuildAgent , . , , Rhino.Mocks . (, , ), Moq , . / () , Rhino.Mocks. Moq , , , ( It. *).

, .

+3

, BuildAgentSelector. . BuildAgentSelector ( / ), , buildServer, teamProjectName . , BuildAgentSelector. .

, , . , , jrista , .

- , . , , - , .

MOQ , .

+1

, . , IBuildServer, IBuildServer.

, .

TypeMock , .

0

Source: https://habr.com/ru/post/1709256/


All Articles