Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 传递到安装程序的Moq rethrow错误_C#_Unit Testing_Nunit_Moq - Fatal编程技术网

C# 传递到安装程序的Moq rethrow错误

C# 传递到安装程序的Moq rethrow错误,c#,unit-testing,nunit,moq,C#,Unit Testing,Nunit,Moq,因此,我有一个使用接口的错误处理类: public interface IEventLogger { void WriteError(Exception ex, string message); } 因此,我正在用它来模拟单元测试。此方法通常只将错误记录到事件查看器中,但对于我的单元测试,我希望它重新显示传递到方法中的异常,即,如果错误传递到此模拟类中,我希望单元测试失败。你知道我该怎么做吗 我走了这么远: var moqIEventLogger = new Mock<

因此,我有一个使用接口的错误处理类:

public interface IEventLogger
  {
    void WriteError(Exception ex, string message);
  }
因此,我正在用它来模拟单元测试。此方法通常只将错误记录到事件查看器中,但对于我的单元测试,我希望它重新显示传递到方法中的异常,即,如果错误传递到此模拟类中,我希望单元测试失败。你知道我该怎么做吗

我走了这么远:

 var moqIEventLogger = new Mock<IEventLogger>();
 moqIEventLogger.Setup(s => s.WriteError(It.IsAny<Exception>(), 
                                           It.IsAny<string>()));
var moqIEventLogger=new Mock();
moqIEventLogger.Setup(s=>s.WriteError(It.IsAny()),
It.IsAny());

但我不确定如何访问原始异常,如果可能的话???

如果希望它只失败,那么使用
抛出
方法,如:

moqIEventLogger
            .Setup(s => s.WriteError(It.IsAny<Exception>(),It.IsAny<string>()))
            .Throws<InvalidOperationException>();
moqIEventLogger
.Setup(s=>s.WriteError(It.IsAny(),It.IsAny())
.抛出();
如果希望它抛出给定为参数异常,请尝试:

moqIEventLogger
            .Setup(s => s.WriteError(It.IsAny<Exception>(),It.IsAny<string>()))
            .Callback((Exception ex, string s) => { throw ex; });
moqIEventLogger
.Setup(s=>s.WriteError(It.IsAny(),It.IsAny())
.Callback((异常ex,字符串s)=>{throw ex;});
在ur设置中添加此选项(已修改)

moqIEventLogger.Setup(s=>s.WriteError(It.IsAny()),
It.IsAny())
.Callback(p=>
{
掷骰子;
});
假设使用方法MyMethod的u hv class A,其中调用WriteError方法的断言应该如下所示:

 Assert.Throws<Exception>(ClassA.MyMethod );
Assert.Throws(ClassA.MyMethod);

最下面的一个就是我要找的。刚刚测试过,就像charmThaty一样,只是抛出了一个新的异常。我想重述原来的。修改后的答案…很抱歉没有阅读完整的问题。谢谢你把它降级:-)那更糟。那不会编译@拉法有答案。
 Assert.Throws<Exception>(ClassA.MyMethod );