模拟idisposable.dispose()方法在C#MOQ中
下面是我需要编写测试用例的我的课程。除了 dispose()
注意:我不允许我修改类dataprovider。
请有人在写作测试用例中使用MOQ框架来帮助我。
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
}
}
Below class is given to me for which I need to write test case. All other test case I was able to write except for Dispose()
Note: I am not allowed to modify the class DataProvider.
Please can someone help me in writing test case for Dispose() method using Moq framework.
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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一些应该解释测试每个步骤的代码。
Here is some code that should explain each step of the test.
@Batesian,@jereenmostert,@nkosi:感谢您的回答。
我根据自己的要求对代码进行了调整,并且奏效了。
@Batesian, @JeroenMostert, @Nkosi: Thanks for your answers.
I tweaked the code as per my requirement and it worked.