我正在使用 xUnit 和 Moq 来编写测试用例。我正在使用下面的代码创建一个 Mock,但遇到了突出显示的问题。
CloudBlockBlob source = null;
AccessCondition sourceAccessCondition = null;
AccessCondition destAccessCondition = null;
BlobRequestOptions options = null;
OperationContext operationContext = null;
CloudBlobContainer container = new CloudBlobContainer(uri);
Task task = null;
DeleteSnapshotOption deleteSnapshotOption = new DeleteSnapshotOption();
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>(uri);
mockCloudBlobContainer.Setup(repo => repo.GetBlockBlobReference("sample")).Returns(new CloudBlockBlob(uri));
var mockBlobServiceProvider = new Mock<BlobServiceProvider>();
mockBlobServiceProvider.Setup(repo => repo.GetBlockBlobContainer("sample")).Returns(new CloudBloblContainer(new Uri("http://mytest")));
Run Code Online (Sandbox Code Playgroud)
该行mockBlobServiceProvider.Setup(repo => repo.GetBlockBlobContainer("sample")).Returns(new CloudBloblContainer(new Uri("http://mytest")));给了我这个错误:
System.NotSupportedException: '不支持的表达式:repo => repo.GetBlockBlobContainer("sample")
不可覆盖的成员(此处:BlobServiceProvider.GetBlockBlobContainer)不得用于设置/验证表达式。
班级 …
我正在使用 xUnit 和 Moq 编写测试用例。
我正在尝试模拟 HttpClient 的 PostAsync(),但出现错误。
下面是用于模拟的代码:
public TestADLS_Operations()
{
var mockClient = new Mock<HttpClient>();
mockClient.Setup(repo => repo.PostAsync(It.IsAny<string>(), It.IsAny<HttpContent>())).Returns(() => Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)));
this._iADLS_Operations = new ADLS_Operations(mockClient.Object);
}
Run Code Online (Sandbox Code Playgroud)
错误:
不支持的表达式:repo => repo.PostAsync(It.IsAny(), It.IsAny()) 不可覆盖的成员(此处:HttpClient.PostAsync)不能在设置/验证表达式中使用。
截屏:
在 .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) => …Run Code Online (Sandbox Code Playgroud) 我正在使用 xUnit 和 Moq 编写测试用例。
我在测试类中使用以下代码来测试catch()另一个类方法
private readonly IADLS_Operations _iADLS_Operations;
[Fact]
public void CreateCSVFile_Failure()
{
var dtData = new DataTable();
string fileName = "";
var mockClient = new Mock<IHttpHandler>();
this._iADLS_Operations = new ADLS_Operations(mockClient.Object);
mockClient.Setup(repo => repo.PostAsync(It.IsAny<string>(), It.IsAny<HttpContent>(), It.IsAny<string>()))
.Returns(() => Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest)));
mockClient.Setup(repo => repo.SendAsync(It.IsAny<HttpRequestMessage>(), It.IsAny<string>()))
.Returns(() => Task.FromResult(new HttpResponseMessage(HttpStatusCode.BadRequest))); // here I want to return Exception instead of BadRequest. How to do that.
Exception ex = Assert.Throws<Exception>(() => this._iADLS_Operations.CreateCSVFile(dtData, fileName).Result);
Assert.Contains("Exception occurred while executing method:", ex.Message);
} …Run Code Online (Sandbox Code Playgroud) 我正在使用 xUnit 和 Moq 编写测试用例。
目前我正在为遥测类编写测试用例。
public class TelemetryClientMock : ITelemetryClientMock
{
public string key { get; set; } //I want to mock key variable.
private TelemetryClient telemetry;
public TelemetryClientMock( )
{
telemetry = new TelemetryClient() { InstrumentationKey = key };
}
public void TrackException(Exception exceptionInstance, IDictionary<string, string> properties = null)
{
telemetry.TrackException(exceptionInstance, properties);
}
public void TrackEvent(string eventLog)
{
telemetry.TrackEvent(eventLog);
}
}
Run Code Online (Sandbox Code Playgroud)
在测试类中,我如何模拟关键变量。我曾经为模拟方法编写以下代码。
[Fact]
public void TrackException_Success()
{
Exception ex=null;
IDictionary<string, string> dict = null;
var reader = new …Run Code Online (Sandbox Code Playgroud)