问题描述
我有以下asp.net页面联系
具有 TestHandlerDemoClass
有一种方法,我想为该方法编写一个单元测试用例,但是当我尝试使用 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");
}
}
}
在上面的ex中我得到了这样的消息: { "请求 在这种情况下不可用"}
here in above ex I got message like {"Request is not available in this context"}
我只是想为方法` public void MyTestMethod ( 页面 mypage ) `,它将"Page mypage"作为参数.
I 'm just trying to write unit test cases for method `publicvoidMyTestMethod(Page mypage)` which takes `Page mypage` as parameter.
怎么做?
SE
推荐答案
public partial class Contact : Page
{
}
public class TestHandlerDemoClass
{
public void MyTestMethod(Page mypage)
{
字符串id = mypage.
Request [" EntityId";] //这里我没有在我的页面中获得请求
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页面请求的单元测试用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!