单元测试EAP Asynchronus WebSerivice调用C#,Moq

时间:2018-03-01 15:37:19

标签: c# web-services unit-testing moq

您好我正在尝试使用异步Web服务调用(asmx)进行单元测试方法。代码如下。问题是以某种方式嘲弄,TaskCompletionSource。我应该使用这种模式吗?有没有办法使它可以测试。

public  async Task<Result> CreateConfAsync(byte[] sesja, Conference conference)
    {
        Result rez = new Result(-1,"error");

        try
        {
            var confsStrXml = ConferenceHelper.createConfsXmlString(conference);

            var tcs = new TaskCompletionSource<WsWynik>();

            _proxy.KonferencjaZapiszCompleted += GetCreateConfAsyncCallBack;
            _proxy.KonferencjaZapiszAsync(sesja, confsStrXml,tcs);

            var wsWynik = await tcs.Task;

            rez.status = wsWynik.status;
            rez.message = wsWynik.status_opis;

            if (rez.status != 0) SesjaExceptionCheck.SesjaCheckThrowIfError(rez.status, rez.message);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            _proxy.KonferencjaZapiszCompleted -= GetCreateConfAsyncCallBack;
        }

        return rez;
    }

    public void GetCreateConfAsyncCallBack(object sender, KonferencjaZapiszCompletedEventArgs e)
    {
        var tcs = (TaskCompletionSource<WsWynik>)e.UserState;
        if (e.Cancelled)
        {
            tcs.TrySetCanceled();
        }
        else if (e.Error != null)
        {
            tcs.TrySetException(e.Error);
        }
        else
        {
            tcs.TrySetResult(e.Result);
        }
    }

我试图模拟TaskCompletionSource,但没有结果。

    [TestMethod]
    public async Task CreateConf_ShouldBeOk()
    {
        var conf = new Mock<Conference>();

        var tcs = new TaskCompletionSource<WsWynik>();
        tcs.SetResult(default(WsWynik));


        _proxy.Setup(x => x.KonferencjaZapiszAsync(_sesja, It.IsAny<string>(),tcs))
            .Raises(mock => mock.KoszykKonferencjaZapiszCompleted += null, new EventArgs());


        ConferenceRepository confRep = new ConferenceRepository(_proxy.Object, _dictRep.Object);

        var res = await confRep.CreateConfAsync(_sesja, conf.Object);

        Assert.IsTrue(1 == 1);
    }

1 个答案:

答案 0 :(得分:0)

需要解决一些问题。

无法模拟任务完成源,因为它是在被测方法中创建的。无论如何都不需要嘲笑。

对模拟代理方法使用参数匹配器,以便在从方法传递值时调用。

对于要引发的事件,需要使用模拟传递正确的事件参数,以便被测系统按照需要运行。

[TestMethod]
public async Task CreateConf_ShouldBeOk() {
    //Arrange
    var conf = new Mock<Conference>();              

    var eventArgs = new KonferencjaZapiszCompletedEventArgs(...) {
        Result = //...,
        //populate the necessary properties
    };

    _proxy
        .Setup(_ => _.KonferencjaZapiszAsync(
            It.IsAny<byte[]>(), 
            It.IsAny<string>(), 
            It.IsAny<TaskCompletionSource<WsWynik>>()
        ))
        .Raises(_ => _.KoszykKonferencjaZapiszCompleted += null, eventArgs);


    var repository = new ConferenceRepository(_proxy.Object, _dictRep.Object);

    //Act
    var result = await repository.CreateConfAsync(_sesja, conf.Object);

    //Assert
    Assert.IsNotNull(result);
    Assert.AreEqual(result.status, expectedStatus);
    Assert.AreEqual(result.message , expectedMessage);

    //assert the expected values of the result members
}