我想寫一個單元測試,其中我的sut(authMock)的依賴項應該拋出帶有特定響應的Webexception(json將相應地在該sut中解析)。不過我有麻煩投擲使用起訂量一樣,在引發WebException:嘲笑Webexception包括Web響應
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