public void OlderThan65_ContextWithSeveralPioneersDeadAndAlive_PioneersCurrentlyAreOlderThan65() { //Arrange. The CallOriginal method marks a mocked method/property call that //should execute the original method/property implementation Pioneers pioneers = Mock.Create <Pioneers>(Behavior.CallOriginal); //Arrange. private _datacontext object Mock.NonPublic.Arrange <IList <AviationPioneer> >(pioneers, "_dataContext") .Returns(DataSource.Pioneers()); //Mock the CalculateAge function used by the OlderThan65 function. //The CalculateAge function is already tested in CalculateAge_* test methods /* You can arrange the CalculateAge function for each pioneers available in the context * like this: * Mock.Arrange(() => Pioneers.CalculateAge(new DateTime(1931, 5, 13))) * .Returns(81); //date of birth * * Mock.Arrange(() => Pioneers.CalculateAge(new DateTime(1982, 5, 20))) * //when set this return value to 66 the result count is 3.. * .Returns(30); * * Mock.Arrange(() => Pioneers.CalculateAge(new DateTime(1930, 8, 5))) * .Returns(82); */ //Or make your own test implementation of the calculateAge function. Mock.Arrange(() => Pioneers.CalculateAge(Arg.IsAny <DateTime>())) .Returns((DateTime birth) => { //Your test implementation here, for example return(2012 - birth.Year); }); //Act. Call the orginal implementation, you already arranged a different //implementation for the _datacontext.. and the CalculateAge function... var result = pioneers.OlderThan65(); //Assert //Based on the test value for calculateAge and the test context //there are two person older than 65 Assert.IsTrue(result.Count() == 2); }
public void OlderThan65_ContextWithSeveralPioneersDeadAndAlive_PioneersCurrentlyAreOlderThan65_NonMocked() { // Arrange. Mock without a mocking framework Pioneers pioneers = new Pioneers(Source.XmlFile); //1st problem, you create a real object, is this xmlfile available??? //Arrange. private _datacontext object PropertyInfo prop = typeof(Pioneers).GetProperty("_dataContext", BindingFlags.NonPublic | BindingFlags.Instance); prop.SetValue(pioneers, DataSource.Pioneers(), null); /* We can't mock the calculate function! * Mock.Arrange(() => Pioneers.CalculateAge(new DateTime(1931, 5, 13))) * .Returns(....*/ /* * * * * * * * * * * * Test will fail after some time.. * * * * * * * * * * * * * * * */ //Act. var result = pioneers.OlderThan65(); //Assert Assert.IsTrue(result.Count() == 2); }