c# - C# Moq 中的模拟 IDisposable.Dispose() 方法

下面的课程是给我的,我需要为此编写测试用例。除了 Dispose() 之外,我能够编写的所有其他测试用例

注意:我不允许修改 DataProvider 类。

请有人可以帮助我使用 Moq 框架为 Dispose() 方法编写测试用例。

public class DataProvider<TInput, TRequest, TResponse, TOutput>:IDataProvider<TInput,TOutput> , IDisposable where TRequest : class, new()
where TResponse : class, new()
{

    private readonly ICompose<TInput, TRequest> requestComp;
    private readonly ICompose<TResponse, TOutput> responseComp;
    private bool disposed;
    private readonly IData idata;

    public DataProvider(IConverter<TInput, TRequest> requestComp,
                           IConverter<TResponse, TOutput> responseComp,
                           IDealer dealer,
                           string passcode)
    {
        this.requestComp = requestComp;
        this.responseComp = responseComp;
        this.idata= dealer.GetCalculatedData(passcode);
    }

    public void Dispose()
    {
        if (this.disposed)
        {
            return;
        }

        this.idata?.Dispose();

        this.disposed = true;
    }

    #endregion - IDispose end


    public void GetData(TInput input)
    {
        //Do something here 
    }
}

回答1

这是一些应该解释测试的每个步骤的代码。

[TestMethod]
public void Test()
{
    // Arrange.
    var mockData = new Mock<IData>();
    var mockDealer = new Mock<IDealer>();

    // Setup mockDealer so that the GetCalculatedData returns the object from mockData.
    mockDealer.Setup(d => d.GetCalculatedData(It.IsAny<string>())).Returns(mockData.Object);
    
    // Create the tested object (the DataProvider) using mocked dependencies.
    var dataProvider = new DataProvider<object, object, object, object>(
        requestComp: It.IsAny<IConverter<object, object>>(), 
        responseComp: It.IsAny<IConverter<object, object>>(), 
        dealer: mockDealer.Object,
        passcode: It.IsAny<string>()
    ); 
    
    // Act.
    dataProvider.Dispose();

    // Assert.
    // Verify that Dispose was called on the IData object.
    mockData.Verify(d => d.Dispose());
}

回答2

@Batesian、@JeroenMostert、@Nkosi:感谢您的回答。

我根据我的要求调整了代码并且它起作用了。

[TestMethod]
public void Test()
{
// Arrange.
var mockData = new Mock<IData>();
var mockDealer = new Mock<IDealer>();
var reqComposeMock = new Mock<IConverter<object, object>>();
var resComposeMock = new Mock<IConverter<object, object>>();

// Setup mockDealer so that the GetCalculatedData returns the object from mockData.
mockDealer.Setup(d => d.GetCalculatedData(It.IsAny<string>())).Returns(mockData.Object);

// Create the tested object (the DataProvider) using mocked dependencies.
var dataProvider = new DataProvider<object, object, object, object>(
    requestComp: reqComposeMock.Object, 
    responseComp: resComposeMock.Object, 
    dealer: mockDealer.Object,
    passcode: "passcode"
); 

// Act.
dataProvider.Dispose();

// Assert.
// Verify that Dispose was called on the IData object.
mockData.Verify(d => d.Dispose());
}

相似文章

c# - 正确的 dispose 派生类

我正在尝试在派生类上实现IDisposable模式,但它并没有像预期的那样工作,假设我有两个类,我想调用派生类的Dispose方法:下面是我的基类的代码publicclassBaseClass:IDi...

最新文章