views:

405

answers:

2

I can't find any info on whether it can do this.

I have a couple of protected methods inside the abstract class and want to test these.

I don't want to inherit from the class and test its implementation (besides thats technically not strictly unit testing, also VS2008 won't let you test inherited methods).

I would like to solve this problem within the context of the integrated unit tests ... I am aware that nUnit for example will allow you to do this.

any thoughts? suggestions?

+1  A: 

Try RhinoMocks. They can create a derived class on the fly (a so called partial mock) where you can call your methods.

You will need a mock framework anyway to write good unit tests. Here is some documentation, which could give you an impression.

Stefan Steinegger
Just been having a read, I will probably use this framework in the future. But before I do that - ie for this project, I am trying to verify that VS2008 does not have the functionality that I require. Thanks for the response!
John Nicholas
+1  A: 

VS2008 can test private and protected methods inherited from base classes, even abstract ones.

If you are happy testing this way then right click on the code to be tested and create unit tests.

Here's an example of a piece of code which I am using to test a private, inherited function, from an abstract class:

    [TestMethod()]
    [DeploymentItem("PA Manager.dll")]
    public void GetNextScheduledTimeTest4()
    {
        TimeWheel timeWheel = new TimeWheel(1000, null);
        AnnouncementSchedule schedule = new AnnouncementSchedule();
        schedule.StartDate = DateTime.UtcNow.Date - TimeSpan.FromDays(1);
        schedule.StartTime = DateTime.Parse("09:00:00");
        schedule.EndDate = DateTime.UtcNow.Date;
        schedule.EndTime = DateTime.Parse("14:30:00");
        schedule.Interval = DateTime.MinValue + TimeSpan.Parse("01:00:00");
        schedule.DaysValid.ValidDay = new Day[7];
        schedule.DaysValid.ValidDay[0] = Day.Monday;
        schedule.DaysValid.ValidDay[1] = Day.Tuesday;
        schedule.DaysValid.ValidDay[2] = Day.Wednesday;
        schedule.DaysValid.ValidDay[3] = Day.Thursday;
        schedule.DaysValid.ValidDay[4] = Day.Friday;
        schedule.DaysValid.ValidDay[5] = Day.Saturday;
        schedule.DaysValid.ValidDay[6] = Day.Sunday;

        //// test for the next interval time NOT being valid, but the time period and day for the schedule is.

        PaAnnouncementSchedule paAnnouncementSchedule = new PaAnnouncementSchedule(timeWheel, schedule);
        PrivateObject param0 = new PrivateObject(paAnnouncementSchedule); 
        PaAnnouncementSchedule_Accessor target = new PaAnnouncementSchedule_Accessor(param0); 
        DateTime currentTime = DateTime.Parse("14:07:00");
        DateTime actual;
        DateTime expected = DateTime.MaxValue;
        actual = target.GetNextScheduledTime(currentTime);
        Assert.AreEqual(expected, actual, "Expected time to be DateTime.MaxValue");
    }

Out of that lot the important part is:

PaAnnouncementSchedule paAnnouncementSchedule = new PaAnnouncementSchedule(timeWheel, schedule);
PrivateObject param0 = new PrivateObject(paAnnouncementSchedule); 
PaAnnouncementSchedule_Accessor target = new PaAnnouncementSchedule_Accessor(param0);

This allows me to access the private inherited function "GetScheduledTime"

ChrisBD
thank you, I was trying something similar ... you have cleared up the syntax nicely for me.
John Nicholas