我搜索了StackOverflow并在Google上搜索了四个小时,仍然没有找到任何解决我的“琐碎”问题的方法。
如果你为你的过滤写单元测试
[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");
}
到目前为止,测试甚至没有因为[authorize]过滤器而命中actionResult方法,引发的异常是:
System.NullReferenceException: Object reference not set to an instance of an object.