如何在VS2008中使用集成单元测试来测试抽象类

发布于 2024-07-20 06:34:41 字数 212 浏览 3 评论 0原文

我找不到任何关于它是否可以做到这一点的信息。

我在抽象类中有几个受保护的方法,想要测试它们。

我不想继承该类并测试其实现(此外,这在技术上不是严格的单元测试,而且 VS2008 也不会让您测试继承的方法)。

我想在集成单元测试的背景下解决这个问题......我知道 nUnit 例如将允许您做到这一点。

有什么想法吗? 建议?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

强者自强 2024-07-27 06:34:42

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

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

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

    [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");
    }

其中重要的部分是:

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

这允许我访问私有继承函数“GetScheduledTime”

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"

热风软妹 2024-07-27 06:34:42

尝试RhinoMocks。 他们可以动态创建一个派生类(所谓的部分模拟),您可以在其中调用您的方法。

无论如何,您都需要一个模拟框架来编写良好的单元测试。 这里有一些文档,可以给你一个印象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文