Sample of RC3 API Usage via NMock Embedded Test

Coordinator
Sep 24, 2007 at 1:32 PM
Edited Jun 19, 2008 at 4:18 PM
UPDATED 6/19/2008 with Rhino.Mocks, Linq and the latest RC 1.4 from Thoughtworks (available in code only for now).

See this smoke test below for basic API:

 

using System;
using System.Collections.Generic;
using System.Linq;
using CCStatistics.Domain.Api;
using CCStatistics.Domain.Api.Interfaces;
using NUnit.Framework;
using Rhino.Mocks;
using TDDViewOutputStatus;
using ThoughtWorks.CruiseControl.Core;
using ThoughtWorks.CruiseControl.Core.Publishers.Statistics;

/// <summary>
/// This is a monolitic 'Smoke Test'. There are many fine grained tests to be writtem!
/// </summary>
[TestFixture]
public class MainTestFixtureBaselineSmokes : AssertionHelper {
private MockRepository mocks;

[TestFixtureSetUp]
public void FixtureSpinUp() {
mocks = new MockRepository();
}

private static List<string> GetMockList(string mockBuildName, int repositoryBuildCount) {
// Add the build name to the array tht is expected to be available for ALL builds
// from the repositoy. Here we use 10

return
Enumerable.Repeat(mockBuildName + " {0}", repositoryBuildCount).Select(
s => String.Format(s, DateTime.Today)).ToList();
}

private static string DefaultDirectory {
get { return Environment.GetEnvironmentVariable("TEMP"); }
}

private static List<Statistic> MockStats {
get {
return
new List<Statistic>(new[] {
new Statistic("AssemblyDependencyRatio",
"//builditem[@AssemblyDependencyRatio]")
});
}
}

private ICCStatsProject SetCurrentProjectMocks(string mockBuildName, int repositoryBuildCount) {
// Create the repository mock
var repository = mocks.Stub<IIntegrationRepository>();
var buildNames = GetMockList(mockBuildName, repositoryBuildCount);

// OK set this up to return our builds
repository.Stub(x => x.GetBuildNames()).Return(buildNames.ToArray());
//x.GetBuildNames().Equals(buildNames)); //.Will(Return.Value(buildNames));

var currentProject = mocks.Stub<ICCStatsProject>();
// Finally, add this mock to our project and we are ready for the next step
currentProject.Stub(x => x.IntegrationRepository).Return(repository);
currentProject.Stub(x => x.StatisticsForPublisher).Return(MockStats);
//ub.On(currentProject).GetProperty("StatisticsForPublisher").Will(Return.Value(stats));

// This app obviously needs to know about where all those nice XML Build files are sitting
// here we just return a temp directory via the helper method
//.Method("BuildLogDirectory").Will(Return.Value(DefaultDirectory()));
currentProject.Stub(x => x.Name).Return(mockBuildName).Repeat.Any();
currentProject.Stub(x => x.BuildLogDirectory).Return(DefaultDirectory).Repeat.Any();
currentProject.Stub(x => x.ArtifactDirectory).Return(DefaultDirectory).Repeat.Any();
currentProject.Stub(x => x.WorkingDirectory).Return(DefaultDirectory).Repeat.Any();

return currentProject;
}

/// <summary>
/// NOTE: This is a FULL CYCLE Mock Smoke test. We need many more finely grained
/// tests. This is just to ensure all is well in the application. Each subcomponent
/// will need to have tests added as this evolves. In the mean time this works
/// well to ensure we fundamentally work and this is called from OUR BuildServer
/// (how META is that?) to ensure this is working.
/// This should be your first stop to undersand this code
/// </summary>
[Test]
public void ShouldSmokeTestProjectAPI() {
// We need a name for this project
const String mockBuildName = "CCStatisticsBuildMock";

// Simulate 100 blegacy build results
const int repositoryBuildCount = 100;

ICCStatsView _view = new MockTDDView();

// We need to create a mock for a basic single CruiseControl project using the provided interface
// In the application we get a project by it being selected from the UI and finding it in the
// list of projects gathered from the config file.

var currentProject = SetCurrentProjectMocks(mockBuildName, repositoryBuildCount);

// OK now it's interesting.. We have our own ICCStatisticsPublisher which is a stand-in for the
// StattisticsPublisher concrete class in CruiseControl. We need to add a new overload
// and obviously make this behave in ways it was not intended to (as we are doing all the legacy builds)
var _statisticsPublisher = mocks.Stub<ICCStatisticsPublisher>();

// Here we use the mock array of stats created above. This is a duplicate of StatisticsForPublisher
// Need to look into consolodating that.
_statisticsPublisher.Stub(x => x.ConfiguredStatistics).Return(MockStats);

_statisticsPublisher.Expect(x => x.ProcesLogFile(mockBuildName, currentProject)).IgnoreArguments().Repeat.
Times(repositoryBuildCount);

// This is the critical stub where our project will return our mocked statistics publisher
currentProject.Stub(x => x.StatsPostPublisher).Return(_statisticsPublisher);

// OK go create a real concreete object with all the mcok scaffolding
var _ProjectStatisticsPresenter = mocks.PartialMock<ProjectStatisticsPresenter>(_view,
currentProject,
_statisticsPublisher,
DefaultDirectory);

//_ProjectStatisticsPresenter.Expect(
// x => x.ProcessLogFile(null, null)).IgnoreArguments().Repeat.Times(repositoryBuildCount);
// Run the unit test
_ProjectStatisticsPresenter.RecalculateStatistics();
}
}