如何编写需要.net页面请求的单元测试用例? [英] How to write unit test cases which needs .net page request?
问题描述
联系
具有 MSTest
项目
它会抛出异常,例如
I have following asp.net Page Contact
and
having TestHandlerDemoClass
which
is having one method I want to write a unit test case for that method but when I tried it using MSTest
project
it throws exception like Request
not available in this context
public partial class Contact : Page
{
}
public class TestHandlerDemoClass
{
public void MyTestMethod(Page mypage)
{
id mypage 请求 "EntityId" //在这里,我没有在Mypage内收到请求 代码>
string id= mypage.
Request["EntityId"]
//here I'm not getting Request inside mypage
My Test Project code -
[TestClass]
public class UnitTest1
{
[TestMethod]
public void NullCheck()
{
try
{
Contact contactPage = new Contact();
TestHandlerDemoClass mydemo = new TestHandlerDemoClass();
mydemo.MyTestMethod(contactPage);
}
catch (Exception ex)
{
Assert.AreEqual(ex.Message, "Id not found");
}
}
}
{ "请求 在这种情况下不可用"}
here in above ex I got message like {"Request
is not available in this context"}
我只是想为方法` 页面 mypage `,它将"Page mypage"作为参数.
I 'm just trying to write unit test cases for method ` public void MyTestMethod(Page mypage) ` which takes `Page mypage` as parameter.
怎么做?
SE
推荐答案
Contact and 具有
MSTest 项目
它会抛出异常,例如请求 在这种情况下不可用
I have following asp.net Page
Contact
and havingTestHandlerDemoClass
which is having one method I want to write a unit test case for that method but when I tried it usingMSTest project
it throws exception likeRequest not available in this context
public partial class Contact : Page
{
}
public class TestHandlerDemoClass
{
public void MyTestMethod(Page mypage)
{
//这里我没有在我的页面中获得请求
string id= mypage.
Request["EntityId"]
//here I'm not getting Request inside mypage
我的测试项目代码-
My Test Project code -
[TestClass]
public class UnitTest1
{
[TestMethod]
public void NullCheck()
{
try
{
Contact contactPage = new Contact();
TestHandlerDemoClass mydemo = new TestHandlerDemoClass();
mydemo.MyTestMethod(contactPage);
}
catch (Exception ex)
{
Assert.AreEqual(ex.Message, "Id not found");
}
}
}
在上面的ex中,我得到了如下消息: {"请求
在此上下文中不可用"}
here in above ex I got message like {"Request
is not available in this context"}
这篇关于如何编写需要.net页面请求的单元测试用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!