0

Unit Testing IsUsingIFrame function

by volkanuzun 14. June 2008 02:40

Yesterday, i showed a function which does a regular expression check on a passed string, and returns true/false. Below is the code again one more time:

public bool IsUsingIFrame(string htmlSource)
        {
            if (htmlSource == string.Empty)
                return false;
            string strRegExIFrameCheck = "<iframe\\s+.*src\\s*=\\s*[\"']http://www.csusb.edu/banner2007/?[\"']";
            Regex regexIFrameCheck = new Regex(strRegExIFrameCheck);
            return regexIFrameCheck.IsMatch(htmlSource);
        } 

So lets unit test this function using ms unit test. Right click on the function name, and then click on create unit tests, click ok again on the next window. Name your project.

Rename the unit test that vs created for you to "CheckWhenThereisNoIframe" , we are going to pass a string that does not have any iframe and test it. below is the simple code for it

 [TestMethod()]
        public void CheckWhenThereisNoIframe()
        {
            Scraper target = new Scraper(); // TODO: Initialize to an appropriate value
            string htmlSource = "THERE IS NO FRAME TAG HERE"; // TODO: Initialize to an appropriate value
            bool expected = false; // TODO: Initialize to an appropriate value
            bool actual;
            actual = target.IsUsingIFrame(htmlSource);
            Assert.AreEqual(expected, actual);
        }

We should unit test the conditions where string is empty, when iframe is there with a wrong pattern, when there is iframe with right pattern, below is my code

[TestMethod()]
        public void CheckWhenThereisNoIframe()
        {
            Scraper target = new Scraper(); // TODO: Initialize to an appropriate value
            string htmlSource = "THERE IS NO FRAME TAG HERE"; // TODO: Initialize to an appropriate value
            bool expected = false; // TODO: Initialize to an appropriate value
            bool actual;
            actual = target.IsUsingIFrame(htmlSource);
            Assert.AreEqual(expected, actual);
        }
        [TestMethod()]
        public void CheckWhenStringIsEmpty()
        {
            Scraper target = new Scraper(); // TODO: Initialize to an appropriate value
            string htmlSource = string.Empty; // TODO: Initialize to an appropriate value
            bool expected = false; // TODO: Initialize to an appropriate value
            bool actual;
            actual = target.IsUsingIFrame(htmlSource);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void CheckWhenIframeExists()
        {
            Scraper target = new Scraper(); // TODO: Initialize to an appropriate value
            string htmlSource = "<iframe src='http://www.csusb.edu/banner2007/'/>"; // TODO: Initialize to an appropriate value
            bool expected = true; // TODO: Initialize to an appropriate value
            bool actual;
            actual = target.IsUsingIFrame(htmlSource);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void CheckWhenThereisIframeIncorrectPattern()
        {
            Scraper target = new Scraper(); // TODO: Initialize to an appropriate value
            string htmlSource = "<iframe src='www.google.com"; // TODO: Initialize to an appropriate value
            bool expected = false; // TODO: Initialize to an appropriate value
            bool actual;
            actual = target.IsUsingIFrame(htmlSource);
            Assert.AreEqual(expected, actual);
        }

 

tomorrow i will try to mock the webcreate method .

Tags:

Comments are closed

Powered by BlogEngine.NET 1.6.0.0
Original Design by Laptop Geek, Adapted by onesoft