任务待定的最小起订量 [英] Moq with Task await
问题描述
由于我已将WCF方法转换为Async,因此单元测试失败,并且我无法找出正确的语法来使它们正常工作.
Since I have converted my WCF methods to Async, my unit tests have failed, and I can't figure out the correct syntax to get them to work.
即时代理类
public interface IClientProxy
{
Task DoSomething(CredentialDataList credentialData, string store);
}
服务等级
public class CredentialSync : ICredentialSync
{
private ICredentialRepository _repository;
private IClientProxy _client;
public CredentialSync()
{
this._repository = new CredentialRepository();
this._client = new ClientProxy();
}
public CredentialSync(ICredentialRepository repository, IClientProxy client)
{
this._repository = repository;
this._client = client;
}
public async Task Synchronise(string payrollNumber)
{
try
{
if (string.IsNullOrEmpty(payrollNumber))
{
.... some code
}
else
{
CredentialDataList credentialData = new CredentialDataList();
List<CredentialData> credentialList = new List<CredentialData>();
// fetch the record from the database
List<GetCredentialData_Result> data = this._repository.GetCredentialData(payrollNumber);
var pinData = this._repository.GetCredentialPinData(payrollNumber);
// get the stores for this employee
var storeList = data.Where(a => a.StoreNumber != null)
.GroupBy(a => a.StoreNumber)
.Select(x => new Store { StoreNumber = x.Key.ToString() }).ToArray();
var credential = this.ExtractCredentialData(data, pinData, payrollNumber);
credentialList.Add(credential);
credentialData.CredentialList = credentialList;
foreach (var store in storeList)
{
//this line causes an Object reference not set to an instance of an object error
await _client.DoSomething(credentialData, store.StoreNumber);
}
}
}
catch (Exception ex)
{
throw new FaultException<Exception>(ex);
}
}
测试类
/// </summary>
[TestClass]
public class SynchTest
{
private Mock<ICredentialRepository> _mockRepository;
private Mock<IClientProxy> _mockService;
[TestInitialize]
public void Setup()
{
... some setups for repository which work fine
}
[TestMethod]
public async Task SynchroniseData_WithOneEmployee_CallsReplicateService()
{
this._mockService = new Mock<IClientProxy>();
this._mockService.Setup(x=>x.DoSomething(It.IsAny<CredentialDataList>(), It.IsAny<string>()));
// arrange
string payrollNumber = "1";
CredentialSync service = new CredentialSync(this._mockRepository.Object, this._mockService.Object);
// act
await service.Synchronise(payrollNumber);
// assert
this._mockService.VerifyAll();
}
错误是在调用ClientProxy.DoSomething
时出现的:
The error is when ClientProxy.DoSomething
is called:
对象引用未设置为对象的实例
Object reference not set to an instance of an object
参数都很好.
如果我将ClientProxy.DoSomething
方法转换为同步方法
(public void DoSomething(...)
)代码可以正常工作,但是我确实需要异步调用它
If I convert my ClientProxy.DoSomething
method to a synchronous method
(public void DoSomething(...)
)the code works fine, but I do need this to be called asynchronously
推荐答案
DoSomething
返回null
而不是返回Task
,因此等待时会得到异常.您需要在构建模拟时指定它应返回Task
.
DoSomething
returns null
instead of returning a Task
, and so you get an exception when awaiting it. You need to specify when building the mock that it should return a Task
.
在这种情况下,您似乎可以简单地使用Task.FromResult
返回一个已完成的任务,因此模拟设置应如下所示:
In this case it seems that you can simply return an already completed task using Task.FromResult
so the mock setup should look like this:
this._mockService.Setup(...).Returns(Task.FromResult(false));
从.Net(4.6)的下一版本开始,您可以像这样使用Task.CompletedTask
:
this._mockService.Setup(...).Returns(Task.CompletedTask);
这篇关于任务待定的最小起订量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!