简体   繁体   English

如何伪造html内容以进行单元测试的方法

[英]how to fake html content to unit test a method

I wrote ac# solution. 我写了ac#解决方案。

I want to write unit tests for two classes: 我想为两个类编写单元测试:

HtmlAgilityPackHtmlProvider , HtmlContentManager HtmlAgilityPackHtmlProviderHtmlContentManager

code can be download from: http://ge.tt/7lDhlFF 可以从以下地址下载代码: 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. 我不确定如何伪造HTML内容并验证其结果。

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 - 如果我应该将其发布在stackoverflow代码审查中-

please comment and I'll remove current post. 请发表评论,我将删除当前帖子。

You should take a look at those techniques: 您应该看看这些技术:

Dependency_injection Dependency_injection

Mock_object 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. 如果您的代码使用某些第三方API,则应将其封装在您自己的类上并对其进行单元测试。

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. 如果不尝试测试第三方API,则必须首先知道您需要测试什么,而不需要测试什么。 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. 当您拥有诸如HTMLProviders之类的类时,在某些情况下(例如,测试将利用它的业务代码)对其进行测试会更有意义。 Unless you are developing an API, you should only care about what makes sense in your business logic. 除非您正在开发API,否则您应该只关心业务逻辑中有意义的部分。

But we need more info to help you. 但是我们需要更多信息来帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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