我搜索了stackoverflow並搜索了幾個小時,但仍然沒有找到任何解決方案來解決我的「微不足道」問題。如何對已應用[Authorize]屬性的控制器方法進行單元測試?
如果您爲已過濾的[Authorize] ActionResult
編寫單元測試,那麼如何解決該問題以假冒該用戶已通過身份驗證?
我有很多的過濾與[Authorize]
ActionResult
方法,我想測試我所有的ActionResult
方法不管它們與[Authorize]
或過濾。
的我是什麼意思一個簡單的例子:
[TestMethod]
public void Create_Get_ReturnsView()
{
// Arrange
var controller = new UserController();
// Act
var result = controller.Create();
// Assert
Assert.IsNotNull(result as ViewResult);
}
[Authorize]
public ActionResult Create()
{
return View("Create");
}
截至目前測試甚至不打,因爲[授權]過濾器,拋出異常的ActionResult的方法是:System.NullReferenceException: Object reference not set to an instance of an object.
有些東西你沒有向我們展示。您顯示的測試不會執行任何*操作過濾器。也許你有代碼,但它不在你的問題。對於初學者來說,錯誤的整個調用堆棧如何? – 2009-12-09 22:11:21