简体   繁体   中英

how to fake html content to unit test a method

I wrote ac# solution.

I want to write unit tests for two classes:

HtmlAgilityPackHtmlProvider , HtmlContentManager

code can be download from: http://ge.tt/7lDhlFF

But fails to write the simplest test.

I'm not sure how to fake Html content and verify the results on it.

I tried:

  [TestClass]
    public class HtmlContentManagerUT
    {

        ////no need, only wrapper
        //[TestMethod]
        //public void GetRootContentAsByteArray_GoogleAsInput_NoException()
        //{
        //   IHtmlProvider htmlProvider = new HtmlAgilityPackHtmlProvider();
        //   var rootContentAsByteArray = htmlProvider.GetRootContentAsByteArray("https://www.google.com/");
        //   Assert.IsTrue(RootContentAsByteArray.SequenceEqual());
        //}


        [TestMethod]
        public void GetRootContentAsByteArray_GoogleAsInput_NoException()
        {
            IHtmlProvider htmlProvider = new HtmlAgilityPackHtmlProvider();
            var externalCss = htmlProvider.GetExternalCss();
            Assert.IsTrue(externalCss.SequenceEqual());
        }
    }

If I should post this in stackoverflow code review -

please comment and I'll remove current post.

You should take a look at those techniques:

Dependency_injection

Mock_object

But I really did not understand what are you trying to do. Are you trying to unit test a code that you downloaded? If so, you should not do it. You should only care about the code you created. If your code uses some third party API you should encapsulate it on your own classes and unit test them.

If are not trying to test a third party API, you have to first know what you need to test and what you do not. When you have such classes like HTMLProviders, it can make more sense to test it under some circumstances such as testing the business code that will make use of it. Unless you are developing an API, you should only care about what makes sense in your business logic.

But we need more info to help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM