Unit Testing IsUsingIFrame function

by volkanuzun 6/13/2008 8:40:00 PM

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:

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



About the author

Volkan Uzun




E-mail me Send mail

Twitter

Calendar

<<  December 2008  >>
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

Flickr Badge

www.flickr.com
This is a Flickr badge showing public photos from volkanuzun. Make your own badge here.

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in