I have an extension method with the following signature (in 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 which calls the first (in BuildAgentSelector class):
public BuildAgent Select(IBuildServer buildServer, string teamProjectName)
{
// Omitted agrument validation
IEnumerable<BuildAgent> serverBuildAgents = buildServer.GetEnabledBuildAgents(teamProjectName);
// Omitted - test doesn't get this far
}
And I am trying to test it using 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: Previous method 'IBuildServer.CreateBuildAgentSpec("TeamProjectName");' requires a return value or an exception to throw.
This is obviously calling the real extension method rather than the test implementation. My next inclination 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 misplaced.
The question is: How do I eliminate this dependency and make the Select method testable?
Note that the extension method and BuildAgentSelector classes are in the same assembly and I would prefer avoiding changing this or having to turn to something besides an extension method, though another mocking framework is something I would consider if I knew it would handle this situation.