MVC+UnitTest????????
???????????? ???????[ 2015/3/12 13:24:10 ] ??????????????? ???????
???????????????У???????????POST??GET??????????????????????????????????????helper??????????????????
1 /// <summary>
2 /// ????HttpContextBase
3 /// </summary>
4 /// <returns></returns>
5 public static HttpContextBase CreateHttpContext(NameValueCollection PostParameter?? NameValueCollection GetParameter)
6 {
7 var context = new Mock<HttpContextBase>();
8 var request = new Mock<HttpRequestBase>();
9 var response = new Mock<HttpResponseBase>();
10 var session = new Mock<HttpSessionStateBase>();
11 var server = new Mock<HttpServerUtilityBase>();
12
13 request.Setup(u => u.QueryString).Returns(GetParameter);
14
15 request.Setup(u => u.Form).Returns(PostParameter);
16 response
17 .Setup(rsp => rsp.ApplyAppPathModifier(Moq.It.IsAny<string>()))
18 .Returns((string s) => s);
19
20 context.Setup(ctx => ctx.Request).Returns(request.Object);
21 context.Setup(ctx => ctx.Response).Returns(response.Object);
22 context.Setup(ctx => ctx.Session).Returns(session.Object);
23 context.Setup(ctx => ctx.Server).Returns(server.Object);
24
25 return context.Object;
26 }
???????????
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using BLL;
using COMMON;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using UI.Controllers;
namespace UiTest.Controllers
{
[TestClass]
public class HomeControllerTest
{
[TestMethod]
public void Index()
{
try
{
var homeController = new HomeController();
NameValueCollection post = new NameValueCollection();
post.Add("Version"?? "10");
NameValueCollection get = new NameValueCollection();
get.Add("bbb"?? "222");
HttpContextBase hcb = Common.MvcContextMockFactory.CreateHttpContext(post?? get);
homeController.ControllerContext = Common.MvcContextMockFactory.CreateControllerContext(homeController??
hcb??
"~/Home/Index"??
"get"??
"DefaultRoute"??
"{controller}/{action}"??
null);
ViewResult result = (ViewResult) homeController.Index();
Assert.AreEqual("Index"?? result.ViewName);
}
catch (Exception ex)
{
Assert.IsFalse(true?? ex.Message);
}
}
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11