无法实例化类的代理。找不到无参数的构造函数

时间:2019-07-11 12:51:33

标签: .net .net-core moq xunit microsoft-fakes

在.Net Core中使用xUnit和Moq编写测试用例时,我面临着突出的问题

我已经使用MSTest Fakes编写了以下测试用例。它按预期工作正常。

 [TestClass]
public class TestBlobServiceProvider
{
    private Common.Interfaces.IBlobServiceProvider _iblobprovider;

    public TestBlobServiceProvider()
    {
        Common.Interfaces.IBlobServiceProvider iblobprovider = new BlobServiceProvider();
        this._iblobprovider = iblobprovider;
    }

    public TestBlobServiceProvider(string storageConnectionString)
    { 

    }


    [TestMethod]
    public void Move_Success()
    {
        using (ShimsContext.Create())
        {
            string sourceContainer = "a";
            string destinationContainer = "s";
            string sourceFileName = "d";
            string destinationFileName = "e";

            Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob sourceFile = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob ();
            Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob destFile = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob();

            Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob.AllInstances.StartCopyCloudBlockBlobAccessConditionAccessConditionBlobRequestOptionsOperationContext = (x, y, z, d, e,s) =>
            { 
                     return "Hi"; 
            };

            Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlob.AllInstances.DeleteDeleteSnapshotsOptionAccessConditionBlobRequestOptionsOperationContext = (x, y, z, d, e) =>
           {

           };

            Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlobClient.AllInstances.GetContainerReferenceString = (x, y) =>
            {
                return new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlobContainer();
            };

            Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlobContainer.AllInstances.CreateIfNotExistsAsync = (x) =>
            {
                return Task.Run(() =>
                {
                   return new bool();
                });
            };

            Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlobContainer.AllInstances.GetBlockBlobReferenceString = (x, y) =>
            {
                return new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob();

            };


            CDM.Common.Fakes.ShimBlobServiceProvider.AllInstances.GetBlockBlobContainerString = (x, y) =>
            {
                return new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlobContainer();
            };

            this._iblobprovider.Move( sourceContainer,  destinationContainer,  sourceFileName,  destinationFileName);

        }

    }
}

但是现在我们需要迁移到.Net Core。因此,由于.Net核心缺少Fakes支持,因此我开始使用xUnit进行测试。

以下代码与xUnit和Moq一起使用

public class TestBlobServiceProvider
{
    private readonly Common.Interfaces.IBlobServiceProvider _iblobprovider;

    public TestBlobServiceProvider()
    {
        Common.Interfaces.IBlobServiceProvider iblobprovider = new BlobServiceProvider();
        this._iblobprovider = iblobprovider;
    }

    [Fact]
    public void Move_Success()
    {
            string sourceContainer = "a";
            string destinationContainer = "s";
            string sourceFileName = "d";
            string destinationFileName = "e";

            var uri = new Uri("https://app.blob.core.windows.net/container/https://app.blob.core.windows.net/container/Accounts/Images/acc.jpg");

            CloudBlockBlob source = null;
            AccessCondition sourceAccessCondition = null;
            AccessCondition destAccessCondition = null;
            BlobRequestOptions options = null;
            OperationContext operationContext = null;
            CloudBlobContainer container = new CloudBlobContainer (uri);
            Task task = null;
            DeleteSnapshotsOption deleteSnapshotsOption = new DeleteSnapshotsOption();


            var mockCloudBlobClient = new Mock<Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient>();
            mockCloudBlobClient.Setup(repo => repo.GetContainerReference("sample")).Returns(container);

            var mockCloudBlobContainer = new Mock<Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer>();
            mockCloudBlobContainer.Setup(repo => repo.GetBlockBlobReference("sample")).Returns(new CloudBlockBlob(uri));

            mockCloudBlobContainer.Setup(repo => repo.CreateIfNotExistsAsync()).Returns(Task.Run(() =>
            {
                return new bool();
            }));


           var mockCloudBlob = new Mock<Microsoft.WindowsAzure.Storage.Blob.CloudBlob>();
            mockCloudBlob.Setup(repo => repo.DeleteAsync(deleteSnapshotsOption, sourceAccessCondition, options, operationContext)).Returns(task);

            var mockCloudBlockBlob = new Mock<Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob>();
            mockCloudBlockBlob.Setup(repo => repo.StartCopyAsync(source, sourceAccessCondition, destAccessCondition, options, operationContext)).ReturnsAsync("Hi");

            Common.Interfaces.IBlobServiceProvider obj = new BlobServiceProvider(mockCloudBlobClient.Object, mockCloudBlobContainer.Object, mockCloudBlob.Object, mockCloudBlockBlob.Object);

            obj.Move(sourceContainer, destinationContainer, sourceFileName, destinationFileName);

        //   this._iblobprovider.Move(sourceContainer, destinationContainer, sourceFileName, destinationFileName);

    }
}

我在

遇到错误
Common.Interfaces.IBlobServiceProvider obj = new BlobServiceProvider(mockCloudBlobClient.Object, mockCloudBlobContainer.Object, mockCloudBlob.Object, mockCloudBlockBlob.Object);`

错误:

  

无法实例化类的代理:   Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient。找不到   无参数构造函数。

如何解决上述问题

2 个答案:

答案 0 :(得分:2)

问题在于 moq 无法创建CloudBlobClient,因为它没有无参数的构造函数。但是 moq 可以在没有无参数构造函数的情况下创建对象,但是您需要提供参数。

最简单的方法是使用this构造函数

public CloudBlobClient (Uri baseUri, System.Net.Http.DelegatingHandler delegatingHandler = null);

并提供Uri

类似的东西:

var mockCloudBlobClient = new Mock<CloudBlobClient>(new Uri("http://mytest"));

答案 1 :(得分:0)

当您模拟实现而不是接口时,经常会出现这种情况。如果一个对象有一个接口,通常模拟那个接口会容易得多。

例如,想象一下这个工人类:

//worker.cs
public class TemplateWorker : ITemplateWorker
    {
        private readonly ITemplateRetriever templateRetriever;
        
        public TemplateWorker(ITemplateRetriever templateRetriever)
        {
            this.templateRetriever = templateRetriever ?? 
               throw new ArgumentNullException(nameof(templateRetriever));
        }
     //..class body
     }

如果你试图模拟这个,你会在这篇文章中得到错误

tests.cs
var mockWorker = Mock.Of<TemplateWorker>();
//!!Can not instantiate proxy of class TestProject.TemplateWorker : 
//!!  Could not find a parameterless constructor.'

然而,如果我模拟接口......没有错误!这很好,因为大多数时候在 mock 中我们只关心这个类如何使用它的依赖项的公共契约中的项目。

var mockWorker = Mock.Of<ITemplateWorker>();

//setup what should happen when I call its method...
Mock.Get(mockWorker).Setup(x=>x.SomeMethod(....//;