简体   繁体   English

如何对创建任务的方法进行单元测试

[英]How to unit testing a method which creates a task

How should the following method be unit tested? 以下方法应如何进行单元测试?

    /// <summary>
    /// Entry point for GUI thread.
    /// </summary>
    public void VerifyDataTypesAsync()
    {
        Task verificationTask = Task.Factory.StartNew(() =>
        {
            VerifyDataManagerAsync();
        });
    }

is it only possible to test via integration testing or is it possible to test that a task was created? 是否只能通过集成测试进行测试,还是可以测试已创建任务?

Thanks for your help 谢谢你的帮助

You can create a Factory for creating this task and use it through seams. 您可以创建一个工厂来创建此任务,并通过接缝使用它。 You can use property seam - it is when you set the instance of your factory to a property of an under test class or you can use constructor seam by giving the factory through a constructor. 您可以使用属性接缝-在将工厂的实例设置为被测类的属性时,或者可以通过向构造函数提供工厂来使用构造函数接缝。 Then you can mock this factory and set it through one of two seams and check does your VerifyDataTypesAsync method call the method of creating your task. 然后,您可以模拟该工厂,并通过两个接缝之一对其进行设置,并检查VerifyDataTypesAsync方法是否调用创建任务的方法。

class YourClass
{
    public YourClass(ITaskFactory factory)
    {}

    public void VerifyDataTypesAsync()
    {
       Task verificationTask = factory.Create(); // you can pass an instance of a delegate as parameter if you need.  
    }
}

class TasksFactory : IFactory
{
   public Task Create()
   {

   }
}

Then in your test you can create a mock object of IFactory and pass it to the constructor of your class then set mock rules for mock to check if the method Create was called. 然后,在测试中,您可以创建IFactory的模拟对象并将其传递给类的构造函数,然后为模拟设置模拟规则,以检查是否调用了Create方法。

If we are talking about NUnit and RhinoMocks it can be looked about this: 如果我们谈论的是NUnit和RhinoMocks,可以这样看:

var mockRepository = new MockRepository();
var mockObject = mockRepository.CreateMock<IFactory>();
var yourClass = new YourClass(mockObject);    


Expect.Call(mockObject.Create);

mockRepository.ReplayAll();

mockObject.VerifyDataTypesAsync()

mockRepository.VerifyAll(); // throw Exception if your VerifyDataTypesAsync method doesn't call Create method of IFactory mock

roughly speaking like this... but it is just one way... 这样粗略地讲...但这只是一种方式...

You could wrap the creation of the task in your own Task factory class and verify that it has been used correctly by mocking it in the unit test. 您可以将任务的创建包装在自己的Task工厂类中,并通过在单元测试中对它进行模拟来验证它是否已正确使用。 For example, create a class like this: 例如,创建一个这样的类:

class MyTaskFactory
{
  public virtual void CreateTask(Action a)
  {
    Task.Factory.StartNew(a);
  }
}

You supply your objects that need to create tasks with one of these factory objects. 您使用这些工厂对象之一提供需要创建任务的对象。 In a unit test, you supply a mocked version instead of the actual factory, and see if the CreateTask method has been called. 在单元测试中,您提供模拟版本而不是实际工厂,并查看是否已调用CreateTask方法。

For more information on mocking, have a look at RhinoMocks or Moq . 有关模拟的更多信息,请查看RhinoMocksMoq

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

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