Я столкнулся с выделенной проблемой при написании тестовых примеров с xUnit и Moq в .Net Core
Я написал тестовый пример ниже, используя 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. Поэтому я начал тестовые случаи с xUnit, поскольку ядро .Net не поддерживает Fakes.
Ниже приведен код с 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. Не удалось найти
конструктор без параметров.
Как решить вышеуказанную проблему