我试图编写一个单元测试,其中我sut的依赖项(authMock)应该抛出一个特定响应的Webexception(JSON将在sut中解析)。然而,我在使用Moq时遇到了抛出Webexception的问题:
Stream responseStream = null;
using (var stringstream = @"{""errocode"": ""35""}".ToStream())
{
responseStream = stringstream;
}
var webresponse = new Mock<WebResponse>();
webresponse.Setup(c => c.GetResponseStream()).Returns(responseStream);
authMock.Setup((x) => x.UserAuthentification(It.IsAny<string>(), It.IsAny<string>())).
Throws(new WebException("fu", null,WebExceptionStatus. TrustFailure, webresponse.Object));
sut.GetUserAuthentification(It.IsAny<string>(), It.IsAny<string>(), (s) => response = s);
//Asserts here
WebException被抛出,但当我尝试在我的sut中捕获它并读取流时,会抛出ArgumentException:
ex.Response.GetResponseStream error CS0103: The name 'ex' does not exist in the current context