簡體   English   中英

使用C#和Moq進行單元測試EAP Asynchronus WebSerivice調用

[英]Unit test EAP Asynchronus WebSerivice call in C# , 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);
    }

需要解決一些問題。

不能完成任務完成源,因為它是在測試方法中創建的。 無論如何都不需要模擬。

對模擬的代理方法使用參數匹配器,以便在傳遞該方法的值時將調用它。

對於引發事件,需要將適當的事件參數與模擬傳遞在一起,以使被測系統能夠按預期運行。

[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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM