posted on Thursday, December 07, 2006 2:55 PM
by
AdrianSpear
Unit Testing Custom NAnt Tasks - Mocking Project object
Just a quickie I hope might be of interest to some and save you a little time.
I recently had cause to write some new custom NAnt tasks for a Cruise Control build I have running. Natch, I wanted to develop the tasks using TDD.
I then found I had need to use the Project object to log some info during the task and following a sample I found on the web duly created:
Project.Log(Level.Error, e.Message);
I figured Project was a static so used TypeMock's MockAll method and ran the test - it failed == object reference not set!
Turns out looking at the NAnt Element object in Reflector that Project is a property - and a public virtual one at that.
Solution == use TypeMock to mock Project, set expectations and assign the object to my task instance - voila!
Mock mockProject = MockManager.Mock(
typeof (Project), Constructor.Mocked);
mockProject.ExpectCall("Log").Args(Check.IsEqual(Level.Error), Check.IsEqual("Foo Test Exception"));
Project mockProjectInstance =
new Project(new XmlDocument(), Level.None, 0);
this.versionInfoCreator.Project = mockProjectInstance;