[TestClass]
public class TFSTests
{
[TestMethod]
public void The_last_completed_and_non_failed_build_can_be_found()
{
// Arrange
// Create the fake TFS server
var fakeTfsServer = Isolate.Fake.Instance<TeamFoundationServer>();
// Swap it in the next time the constructor is run
Isolate.Swap.NextInstance<TeamFoundationServer>().With(fakeTfsServer);
// Create a fake build server instance
var fakeBuildServer = Isolate.Fake.Instance<IBuildServer>();
// Set the behaviour on the TFS server to return the build server
Isolate.WhenCalled(() => fakeTfsServer.GetService(typeof(IBuildServer))).WillReturn(fakeBuildServer);
// Create some test data for the build server to return
var fakeBuildDetails = CreateResultSet(new List<BuildTestData>() {
new BuildTestData() {BuildName ="Build1", BuildStatus = BuildStatus.Failed},
new BuildTestData() {BuildName ="Build2", BuildStatus = BuildStatus.PartiallySucceeded},
new BuildTestData() {BuildName ="Build3", BuildStatus = BuildStatus.Failed},
new BuildTestData() {BuildName ="Build4", BuildStatus = BuildStatus.Succeeded},
new BuildTestData() {BuildName ="Build5", BuildStatus = BuildStatus.PartiallySucceeded},
new BuildTestData() {BuildName ="Build6", BuildStatus = BuildStatus.Failed}
});
// Set the behaviour on the build server to return the test data, the nulls mean we don’t care about parameters passed
Isolate.WhenCalled(() => fakeBuildServer.QueryBuilds(null, null)).WillReturn(fakeBuildDetails);
// Act
// Call the method we want to test, as we are using a fake server the parameters are actually ignored
var actual = TFSMocking.BuildDetails.GetLastNonFailingBuildDetails("http://FakeURL:8080/tfs", "FakeTeamProject", "FakeBuildName");
// Assert
Assert.AreEqual("Build5", actual.BuildNumber);
}
/// <summary>
/// A helper method to hide the Typemock code used to create each build results set
/// </summary>
/// <param name="builds">The parameters to populate into the build results</param>
/// <returns>A set of build results</returns>
private IBuildDetail[] CreateResultSet(List<BuildTestData> builds)
{
var fakeBuilds = new List<IBuildDetail>();
foreach (var build in builds)
{
// Create a fake build result instance
var fakeBuildDetails = Isolate.Fake.Instance<IBuildDetail>();
// Set the properties, in this sample we only set a couple of properties, but this can be extended
fakeBuildDetails.BuildNumber = build.BuildName;
fakeBuildDetails.Status = build.BuildStatus;
fakeBuilds.Add(fakeBuildDetails);
}
return fakeBuilds.ToArray();
}
}
/// <summary>
/// A holding class for the build data we are interested in faking
/// </summary>
public class BuildTestData
{
public string BuildName {get;set;}
public BuildStatus BuildStatus {get;set;}
}