1

我想替换一个异常和它的字段。

像这样的东西:

var webExcetion = Substitute.For<WebException>();
webExcetion.Response.Returns(httpWebResponse);
substituteForHttp.GetResponse(Arg.Any<string>()).Returns(x => { throw webExcetion; });

此代码由 NSubstitute 引发 Castle.Proxies.ExceptionProxy 或 NSubstitute.Exceptions.CouldNotSetReturnException。

我怎样才能做到这一点?

4

1 回答 1

4

该类WebException没有虚拟成员,因此 NSubstitute 不能用它做很多事情(它通过使用 Castle DynamicProxy 创建派生类型的实例来工作,然后通过覆盖所有虚拟成员将实例更改为替代工作)。

在这种情况下,使用 real 来解决这个问题应该没问题WebException

WebException webException = 
    new WebException("test", null, webExceptionStatus, httpWebResponse);

这将根据需要将Response属性设置httpWebResponse为。

希望这可以帮助。

于 2012-08-31T23:23:04.770 回答