locked
How to write unit test cases which needs .net page request? RRS feed

  • Question

  • 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) {

    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"}

    I 'm just trying to write unit test cases for method ` public void MyTestMethod(Page mypage) ` which takes `Page mypage` as parameter.

    how to do it?


    SE

    Sunday, November 6, 2016 3:22 PM

All replies

  • 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) {

    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"}


    SE




    Saturday, November 5, 2016 11:01 AM
  • Saturday, November 5, 2016 8:59 PM
  • I 'm just trying to write unit test cases for method ` public void MyTestMethod(Page mypage) ` which takes `Page mypage` as parameter.

    how to do it?



    SE

    Sunday, November 6, 2016 4:47 AM
  • I 'm just trying to write unit test cases for method ` public void MyTestMethod(Page mypage) ` which takes `Page mypage` as parameter.

    how to do it?



    SE

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vsunittest
    Sunday, November 6, 2016 2:28 PM
  • Hi SE,

    >>I tried it using MSTest project it throws exception like Request not available in this context

    Whether you mean you test this method with command line? I have tested your coded in VS IDE and I have encountered the following issue.

    Could you please share me what you want to test, you have added a class to the Contact.aspx and in this class you have added method, and the method need to call Contact variable. So I want to know what you want to test. Please share me how to get the “id”, what’s the value of this id, and where to call the MyTestMethod (not in unit test) successfully.

    And if you just want to judge the exception message, I think we could use the following code in you catch statement:

    Assert.AreEqual(ex.Message,"Request is not available in this context"); 
    If I misunderstand your issue, please feel free to let me know.

    Sincerely,

    Oscar


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    • Edited by Oscar_Wu Tuesday, November 8, 2016 9:17 AM
    • Proposed as answer by Oscar_Wu Thursday, November 10, 2016 2:32 AM
    Monday, November 7, 2016 8:41 AM