简体   繁体   English

我应该将“自定义单元测试声明”类设为非静态吗?

[英]Should I make my Custom Unit Test Assert Class Non-Static?

I use the built in Visual Studio Unit Testing Tools to perform unit tests against CRM 2011. A lot of the tests are Asserting the creation of an Entity. 我使用内置的Visual Studio单元测试工具针对CRM 2011执行单元测试。许多测试都在断言一个实体的创建。

Assert.IsNull(service.GetFirstOrDefault<Contact>(contact.Id));

(the GetFirstOrDefault is an extension method that attempts to retrieve the contact by id from CRM, and returns null if not found) (GetFirstOrDefault是一种扩展方法,它尝试通过id从CRM检索联系人,如果找不到,则返回null)

I'd like to create my own Assert method that operates like this: 我想创建自己的Assert方法,其操作如下:

CrmAssert.Exists(service, contact);

At first I thought it would be a good idea to inherit from Assert , but alas, it is a static class. 起初,我认为从Assert继承是个好主意,但是,这是一个静态类。 I then created a new static class like so: 然后,我创建了一个新的静态类,如下所示:

public static class AssertCrm
{
    public static void Exists<T>(IOrganizationService service, T entity) where T : Entity
    {
        Exists(service, entity, null, null);
    }
    public static void Exists<T>(IOrganizationService service, T entity, string message) where T : Entity
    {
        Exists(service, entity, message, null);
    }

    public static void Exists<T>(IOrganizationService service, T entity, string message, params object[] parameters) where T: Entity
    {
        if (service.GetFirstOrDefault<T>(entity.Id) == null)
        {
            throw new AssertFailedException(message == null ? String.Format(message, parameters));
        }
    }
}

Called like so: 这样称呼:

AssertCrm.Exists(service, contact);

This is fine, except that I really should be able to set the service, once, and not have to call it each time: 很好,除了我真的应该能够一次设置服务,而不必每次都调用它:

AssertCrm.Service = service;
AssertCrm.Exists(contact);
AssertCrm.Exists(campaign);
AssertCrm.Exists(etc...);

But I believe that Visual Studio will attempt to run tests multi-threaded, which means that when I set the Service static property, it could be overridden by a different service in a different test (As well as IOrganizationService not being thread-safe). 但是我相信Visual Studio会尝试运行多线程的测试,这意味着当我设置Service静态属性时,它可能会在不同的测试中被其他服务覆盖(以及IOrganizationService不是线程安全的)。

Do I need to make my AssertCrm class non-static so I don't have to worry about mutli-threading? 我是否需要使我的AssertCrm类成为非静态类,所以不必担心多线程处理? Is there a simpler technique that I'm missing? 我缺少一种更简单的技术吗?

您可以将AssertCrm设为单例,然后实例化一次,所有其他调用将使用同一实例。

Personally I prefer the first option with the use of the built in assertions library. 我个人更喜欢使用内置断言库的第一种选择。 I don't see what benefit you're getting from trying to wrap this in an extension method or "helper". 我看不到尝试将其包装在扩展方法或“帮助程序”中有什么好处。

If you really want to go down this route you could create a set of extension methods on IOrganizationService to remove the need to keep passing it in. Makes it a bit cleaner but I'm still in favour of the first. 如果您真的想走这条路,则可以在IOrganizationService上创建一组扩展方法,以消除继续传递它的需要。这使它更IOrganizationService ,但我仍然赞成第一个。

crmService.AssertExists(contact);

As an aside, why are you hitting a real CRM instance in your "unit" tests? 顺便说一句,为什么要在“单元”测试中创建一个真正的CRM实例? Much quicker and more maintainable to verify against a mock IOrganizationService within your tests. 在测试中针对模拟IOrganizationService进行验证的过程更快,更易于维护。

After some additional research I didn't find any reason that it should (has to) be a static class, and have since created it as a non-static class. 经过一些额外的研究,我没有发现它应该(必须)为静态类的任何原因,并因此将其创建为非静态类。

One note to anyone else thinking of doing something similar: Do create static method overloads for all of your instance methods since ClassName.AssertMethod() really does make more sense a majority of the time vs new ClassName().AssertMethod() 给其他想做类似事情的人的便条:为所有实例方法创建静态方法重载,因为ClassName.AssertMethod()new ClassName().AssertMethod()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM