Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用调用另一个方法的后台工作程序对void方法进行单元测试?_C#_Unit Testing_Visual Studio 2012_Refactoring - Fatal编程技术网

C# 使用调用另一个方法的后台工作程序对void方法进行单元测试?

C# 使用调用另一个方法的后台工作程序对void方法进行单元测试?,c#,unit-testing,visual-studio-2012,refactoring,C#,Unit Testing,Visual Studio 2012,Refactoring,请记住,我对单元测试还很陌生,所以请耐心听我说。我有一个方法,它使用后台工作程序调用一个方法,该方法的类在我的解决方案中的另一个项目中,我发现很难集中精力对它进行单元测试 下面是该方法的基本操作: public void VerifyLocation() { this.IsBusy = true; using (BackgroundWorker worker = new BackgroundWorker()) { worker.DoWork += (se

请记住,我对单元测试还很陌生,所以请耐心听我说。我有一个方法,它使用后台工作程序调用一个方法,该方法的类在我的解决方案中的另一个项目中,我发现很难集中精力对它进行单元测试

下面是该方法的基本操作:

public void VerifyLocation()
{
    this.IsBusy = true;

    using (BackgroundWorker worker = new BackgroundWorker())
    {
        worker.DoWork += (sender, e) =>
        {
            Context context = Profile.GetContext();

            AssetManagerService svc = new AssetManagerService(context);
            this.Status = svc.Login();

            if (this.Status == AssetManagerLoginStatus.Success)
            {
                //Do stuff
                this.ShowError = false;
            }
            else if (this.Status == AssetManagerLoginStatus.LocationDoesNotExst || this.Status == AssetManagerLoginStatus.LibraryTitleNotDetermined)
                this.ShowError = true;
            else
                this.ShowError = false;
        };

        worker.RunWorkerCompleted += (sender, e) =>
        {
            if (e.Error != null)
            {
                this.ShowError = true;
            }

            this.IsBusy = false;
        };

        worker.RunWorkerAsync();
    }
}
下面是AssetManager服务的示例:

public class AssetManagerService
{
    //AssetManager is an abstract class
    private AssetManager provider = null;

    public AssetManagerService(UnityContext context)
    {
        this.Context = context;
        this.InitializeProvider();
    }

    private void InitializeProvider()
    {
        //Determine provider based on Context properties
    }

    public AssetManagerLoginStatus Login()
    {
        return this.provider.Login();
    }
}

显然,我最关心的是登录方法被调用。我是否会使用垫片来控制定位销工作过程中发生的情况?我会使用存根强制登录返回特定值吗?这两个概念对我来说都是全新的。是否需要进行重构以使其更易于单元测试?如果是这样,需要做哪些更改?

我会将DoWork移到一个单独的方法中,因为这样测试它比尝试测试异步/多线程代码容易得多

如果您只需要测试登录名是否被称为mock/shim,那么这可能是最好的方法。此外,通过构造函数将AssetManager模拟注入AssetManager服务实例会更容易


此外,模拟接口更容易。也许您可以从AssetManager中提取接口,对其进行模拟,并通过构造函数将其注入AssetManager实例。要模拟接口,不一定需要垫片。一个更简单的模拟框架,如NSubstitute,可以完成这项工作。使用NSubstitute,您还可以告诉您的mock在调用Login时返回特定值。

您使用的Visual Studio版本是什么?似乎是Fakes女士的好人选。