Chi*_*rac 4 c# moq transactionscope
有没有办法moq一个事务范围,以确保在进程中调用提交,完成或回滚操作?
鉴于此功能:
public void DoSomething(int param)
{
using (TransactionScope transactionScope = new TransactionScope())
{
factory.DoSomething(param);
factory.DoSomethingElse(param);
transactionScope.Complete();
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法通过IDisposable接口调用Verify函数?它似乎只包含Dispose函数,我想测试Commit或Rollback.
谢谢
TransactionScope是密封类,仅从IDisposable接口派生.在我考虑的时候,你需要创建自己的包装类TransactionScope 和接口,这样你就可以使用和模拟它.
class TransScopeWrapper : ITransScope, IDisposable
{
private TransactionScope scope;
public TransScopeWrapper()
{
scope = new TransactionScope();
}
public void Dispose()
{
scope.Dispose();
}
public void Complete()
{
scope.Complete();
}
public void Rollback()
{
...
}
}
internal interface ITransScope
{
void Complete();
void Rollback();
}
Run Code Online (Sandbox Code Playgroud)
你还需要以ITransScope某种方式注入模拟.我会按照以下方式进行
public void DoSomething(int param)
{
using (TransScopeWrapper transactionScope = new TransScopeWrapper ())
{
DoSomething(param, transactionScope);
}
}
internal void DoSomething(int param, ITransScope transaction)
{
factory.DoSomething(param);
factory.DoSomethingElse(param);
transaction.Complete();
}
Run Code Online (Sandbox Code Playgroud)
所以现在你可以测试DoSomething(int param, ITransScope transaction)方法嘲笑了ITransScope.
| 归档时间: |
|
| 查看次数: |
830 次 |
| 最近记录: |