【问题标题】:Proper Way to Test a file method with .net使用 .net 测试文件方法的正确方法
【发布时间】:2020-06-22 18:20:16
【问题描述】:

我是 .net 和测试新手。我的以下代码如下所示:

using System.Xml.Linq;
    public class AnimalXmlService
    {
        public Animal GetAnimalInfoFromXml(string url) {
            XElement xml_doc = GetXmlInfo(url);
            if (xml_doc == null)
            {
                return null;
            } else { 
                XElement animal_info = xml_doc.Element("Animal");
                string animal_name = GetAnimalName(animal_info);
                int animal_id = GetAnimalId(animal_info);

                return new Animal(animal_id, animal_name);
            }
        }

        private XElement GetXmlInfo(string url)
        {
            try
            {
                XElement animal_xml_info = XElement.Load(url);
                return animal_xml_info;
            }
            catch
            {
                return null;
            }
        }


        private int GetAnimalName(XElement animal_info)
        {
           ....
        }

} 

我的问题是如何存根 GetAnimalInfoFromXml 以返回文件?我有我将使用的示例 xml 文件,而不是发出请求。这是我的以下测试。我也想知道是否有更好的方法来重构这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;

namespace AnimalXmlService
{
    [TestFixture]
    public class AnimalXmlTest
    {

        [Test]
        public void extracts_valid_name()
        {
           //get xml file?

            animalService AnimalXmlService = new AnimalXmlService();
            name = animalService.GetAnimalName(xml_file);
            Assert.AreEqual(name, "bobby the tiger");
        }

        [Test]
        public void extracts_valid_id()
        {
           //get xml file?
           xml_file = fetch_file //is this appropriate?

            animalService AnimalXmlService = new AnimalXmlService();
            id = animalService.GetAnimalId(xml_file);
            Assert.AreEqual(name, "2");
        }
    }
}

【问题讨论】:

标签: c# .net testing nunit


【解决方案1】:

在这种情况下,您可以使用测试替身。 首先,你应该让你的代码更易测试(打破依赖)

public class AnimalXmlService
{
    private readonly IXmlReader _xmlReader;

    public AnimalXmlService(IXmlReader xmlReader)
    {
        this._xmlReader = xmlReader;
    }

    public Animal GetAnimalInfoFromXml(string url)
    {
        XElement xml_doc = _xmlReader.Load(url);
        if (xml_doc == null)
        {
            return null;
        }
        else
        {
            XElement animal_info = xml_doc.Element("Animal");
            string animal_name = GetAnimalName(animal_info);
            int animal_id = GetAnimalId(animal_info);

            return new Animal(animal_id, animal_name);
        }
    }
}

然后你应该创建一个存根来替换你真正的依赖。 (您也可以使用 NSubstitute、Mock 等框架...)

public class XmlReaderStub : IXmlReader
{
    public XElement XElement { get; set; }

    public XElement Load(string url)
    {
        return XElement;
    }
}

最后

public class AnimalXmlTest
{
        [Test]
        public void extracts_valid_name()
        {
            var stub = new XmlReaderStub();
            stub.XElement = new XElement(); // some XElement
            animalService AnimalXmlService = new AnimalXmlService(stub);

            name = animalService.GetAnimalName();

            Assert.AreEqual(name, "bobby the tiger");
        }
}

【讨论】:

  • 谢谢!您认为我应该在哪里添加 xmlReaderstub?
  • AnimalXmlTest 中的 xml 文件在哪里?
  • 你应该在你的测试项目中添加 XmlReaderStub。
  • 我最终做了一些稍微不同的事情,但打破依赖的事情让我朝着正确的方向前进。谢谢!
【解决方案2】:

您可以在您的类中使用另一种方法,如以下返回 XmlDocument 的方法。

 public XmlDocument GetXmlFile()
   {      
      XmlDocument doc = new XmlDocument();
      doc.LoadXml("<Animal><Name>Bobby the tiger</Name></Animal>");

      return doc;
   }

【讨论】:

    猜你喜欢
    • 2011-08-09
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 2023-03-10
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多