我們試圖嘲弄使用RhinoMocks 一個MVC 3應用程序的HttpContext的複合材料部件看起來像這樣:建立一個結果爲一個模擬
HttpContextBase context = mocks.StrictMock<HttpContextBase>();
HttpRequestBase request = mocks.PartialMock<HttpRequestBase>();
IPrincipal user = mocks.StrictMock<IPrincipal>();
HttpCookieCollection cookies = new HttpCookieCollection();
IIdentity identity = mocks.StrictMock<IIdentity>();
HttpResponseBase response = mocks.PartialMock<HttpResponseBase>();
SetupResult.For(response.Cookies).Return(cookies);
SetupResult.For(context.User).Return(user);
SetupResult.For(user.Identity).Return(identity);
SetupResult.For(context.Request).Return(request);
SetupResult.For(context.Response).Return(response);
mocks.Replay(context);
在我的測試,我需要的用戶可以使認證我補充如下:「爲IIdentity.get_IsAuthenticated()的結果;已設置了」
var identity = context.User.Identity;
mocks.BackToRecord(identity);
SetupResult.For(identity.IsAuthenticated).Return(true).Repeat.Any();
mocks.Replay(identity);
然而,這導致 異常被拋出。
爲什麼?我需要做什麼才能在我的測試中進行認證設置?
@帕特里克:我看到你編輯了我的問題,你的改變是什麼性質?我找不到任何權利。 – 2011-12-20 08:29:15
如果您點擊「編輯xxx小時前」的時間鏈接,您可以查看修訂歷史記錄(帶註釋)。您會注意到我在第一個代碼示例中修復了代碼格式。 – PatrickSteele 2011-12-20 13:05:25