我有以下Action Method
我正在尝试进行moq测试.请注意AcceptVerbs
?我需要确保我正在测试它.
这是方法.
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create([Bind(Include = "Subject, Content")]Post post, HttpPostedFileBase imageFileName) { ... }
这是我的moq代码......
[TestMethod] public void Create_Action_snip_sniop_When_Http_Post_Is_Succesful() { // Arrange. var mock = new Mock(); mock.SetupGet(m => m.HttpContext.Request.HttpMethod).Returns("POST"); // Snip some other arrangements. var controller = PostController; controller.ControllerContext = mock.Object; // Act. var viewResult = controller.Create(post, image.Object) as ViewResult; // Assert. Assert.IsNotNull(viewResult); // TODO: Test that the request was an Http-Post.
我需要做些什么来验证请求是一个帖子?
作为单元测试运行时,不会调用您的属性,因为它通常由ControllerActionInvoker作为Mvc"堆栈"的一部分调用.我在这种情况下所做的是编写测试以确保使用正确的参数将正确的属性应用于操作.然后我相信框架将正确地完成其工作.
这样做需要反思:
public void Only_posts_are_allowed_to_my_action() { var method = typeof(MyController).GetMethod("MyAction"); var attribute = method.GetCustomAttributes(typeof(AcceptVerbsAttribute),false) .Cast() .SingleOrDefault(); Assert.IsNotNull( attribute ); Assert.AreEqual( 1, attributes.Count() ); Assert.IsTrue( attributes.Contains( HttpVerbs.Post ) ); }