I have an extension method with the following signature (in the BuildServerExtensions class):
public static IEnumerable<BuildAgent> GetEnabledBuildAgents(
this IBuildServer buildServer,
string teamProjectName)
{
var buildAgentSpec = buildServer.CreateBuildAgentSpec(teamProjectName);
}
And another method that calls the first (in the BuildAgentSelector class):
public BuildAgent Select(IBuildServer buildServer, string teamProjectName)
{
IEnumerable<BuildAgent> serverBuildAgents =
buildServer.GetEnabledBuildAgents(teamProjectName);
}
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 , -, , - , , , .