public void SetupTests()
{
moqsession = new Mock<HttpSessionStateBase>();
moqContext = new Mock<HttpContextBase>();
moqRequest = new Mock<HttpRequestBase>();
moqContext.Setup(x => x.Request).Returns(moqRequest.Object);
moqContext.Setup(x => x.Session).Returns(moqsession.Object);
// Create a "fake" form
formValues = new NameValueCollection
{
{ "HiddenTransactionNumber", "0" },
{ "LastName", "Danylko" }
};
}
public void Test_withModel()
{
moqRequest.Setup(r => r.Form).Returns(formValues);
var forms = moqContext.Object.Request.Form;
// Assert
Assert.IsNotNull(forms);
ControllerContext ctx = new ControllerContext();
ctx.HttpContext = moqContext.Object;
var i = new Mock<IController>();
i.Setup(d => d.FillViewBag());
var A = new Model();
A.Tno = Convert.ToInt64(0);
var controller = new Controller(i.Object);
controller.ControllerContext = ctx;
controller.Index(A);
}
上面是moq Request.Forms的单元测试代码。但是,当我使用它获取运行时异常作为System.Argument。在Controller中,我的代码是myRequest.Form [“HTN”]