Unit testing 基于mvc的actionfilter单元测试
我正在尝试为actionfilter编写一个单元测试。我写了一些代码,但我不知道这是不是真的。我的操作结果代码如下:Unit testing 基于mvc的actionfilter单元测试,unit-testing,action-filter,Unit Testing,Action Filter,我正在尝试为actionfilter编写一个单元测试。我写了一些代码,但我不知道这是不是真的。我的操作结果代码如下: public override void OnActionExecuting(ActionExecutingContext filterContext) { if (!HttpContext.Current.User.Identity.IsAuthenticated) { HttpContext.Current.Response.Redirect("/
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!HttpContext.Current.User.Identity.IsAuthenticated)
{
HttpContext.Current.Response.Redirect("/kullanicigiris");
}
}
我将此actionresult添加到一些控制器的顶部
[AuthenticationFilter]
public class HomeController : Controller
我编写了如下测试代码:
[TestMethod]
public void TestActionFilter()
{
const string expectedViewName = "Create";
const string username = "deneme";
// MockRepository mock=new MockRepository();
AccountController v=new AccountController();
var context = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
context.SetupGet(p => p.User.Identity.Name).Returns(username);
context.SetupGet(p => p.Request.IsAuthenticated).Returns(false);
context.VerifyAll();
}
}
[TestMethod]
公共void TestActionFilter()
{
常量字符串expectedViewName=“Create”;
常量字符串username=“deneme”;
//MockRepository mock=新建MockRepository();
AccountController v=新的AccountController();
var context=newmock();
var request=newmock();
SetupGet(p=>p.User.Identity.Name).Returns(username);
SetupGet(p=>p.Request.IsAuthenticated)。返回(false);
context.VerifyAll();
}
}
我写这篇文章是为了成功。我还将为失败的学生再写一篇。这是一种真正的方法吗