如何在VS2008中使用集成的单元测试来测试抽象类 [英] How to use integrated unit testing in VS2008 to test abstract class

查看:89
本文介绍了如何在VS2008中使用集成的单元测试来测试抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到有关它是否可以执行此操作的任何信息。



我在抽象类中有几个受保护的方法,想要对其进行测试。 / p>

我不想从该类继承并测试其实现(除了从技术上讲并不是严格的单元测试,VS2008也不允许您测试继承的方法)。 / p>

我想在集成单元测试的上下文中解决此问题...我知道,例如,nUnit允许您执行此操作。



有什么想法吗?

解决方案

VS2008可以测试从基类(甚至是抽象类)继承的私有方法和受保护方法。



如果您乐于以这种方式进行测试,则右键单击要测试的代码并创建单元测试。



这里是一个示例我用来测试抽象类的私有继承函数的代码:

  [TestMethod()] 
[DeploymentItem( PA Manager.dll)]
public void GetNextScheduledTimeTest4()
{
TimeWheel timeWheel = new TimeWheel(1000,null);
AnnouncementSchedule时间表= 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;

////测试下一个间隔时间无效,但是时间表的时间段和日期是有效的。

PaAnnouncementSchedule paAnnouncementSchedule =新的PaAnnouncementSchedule(timeWheel,schedule);
PrivateObject param0 =新的PrivateObject(paAnnouncementSchedule);
PaAnnouncementSchedule_Accessor目标=新的PaAnnouncementSchedule_Accessor(param0);
DateTime currentTime = DateTime.Parse( 14:07:00);
DateTime实际;
预期的DateTime = DateTime.MaxValue;
实际= target.GetNextScheduledTime(currentTime);
Assert.AreEqual(预期的,实际的,预期的时间为DateTime.MaxValue);
}

重要的部分是:

  PaAnnouncementSchedule paAnnouncementSchedule =新的PaAnnouncementSchedule(timeWheel,schedule); 
PrivateObject param0 =新的PrivateObject(paAnnouncementSchedule);
PaAnnouncementSchedule_Accessor目标=新的PaAnnouncementSchedule_Accessor(param0);

这使我可以访问私有继承的函数 GetScheduledTime


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?

解决方案

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"

这篇关于如何在VS2008中使用集成的单元测试来测试抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆