簡體   English   中英

如何模擬返回void但修改傳入的引用類型的方法?

[英]How to mock a method that returns void but modifies the reference type passed in?

我有一個像 -

public interface ILedgerStoreRepository
{
    void AddInvoiceDetailsToBatchWriteObj(ref BatchWrite<InvoiceDetailsDAO> batchWriteDetails,
        ref InvoiceDetailsDAO invoiceDetails);
}

我想以這樣一種方式模擬這個函數,我只想驗證invoiceDetails值作為我的驗證。 目前我正在嘗試 -

    var repoMock = new Mock<ILedgerStoreRepository>();
    repoMock.Setup(m =>
        m.AddInvoiceDetailsToBatchWriteObj(ref batchWriteObj1,
            ref It.Ref<InvoiceDetailsDAO>.IsAny)).Callback<BatchWrite<InvoiceDetailsDAO>, InvoiceDetailsDAO>(
        (write, dao) =>
        {
            Assert.Equal(LedgerStoreTestConstants.TestInvoiceDetailsMappingDao, dao);
        }).Verifiable();

但這給我帶來了以下錯誤-

System.ArgumentException 無效的回調。 帶參數的方法設置(ref BatchWrite、ref InvoiceDetailsDAO)不能調用帶參數的回調(BatchWrite、InvoiceDetailsDAO)。

誰能幫我這個?

我稍微修改了輸入參數,以便能夠對其進行測試。

public interface ILedgerStoreRepository
{
        void AddInvoiceDetailsToBatchWriteObj(ref List<string> batchWriteDetails, ref string invoiceDetails);
}

public class TestClass
{
    public void Execute(ILedgerStoreRepository repo)
    {
        List<string> a = new List<string> { "Test", "Alpha" };
        string b = "Alpha";
        repo.AddInvoiceDetailsToBatchWriteObj(ref a, ref b);
    }
}

// You need this delegate
delegate void AddInvoiceCallback(ref List<string> batchWriteDetails, ref string invoiceDetails);

[Test]
public void DummyTest()
{
    var repoMock = new Mock<ILedgerStoreRepository>();
    repoMock.Setup(m =>
                     m.AddInvoiceDetailsToBatchWriteObj(ref It.Ref<List<string>>.IsAny,
                                                              ref It.Ref<string>.IsAny))
                .Callback(new AddInvoiceCallback((ref List<string> batch, ref string details)
            =>
            {
                Assert.That(batch.Contains("Test"));
            })).Verifiable();

    // Act   
    new TestClass().Execute(repoMock.Object);
}

暫無
暫無

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

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