0

I'm new to .net and testing. My following code looks like this:

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)
        {
           ....
        }

} 

My question is how do I stub the GetAnimalInfoFromXml to return a file? I have the sample xml file that I will be using instead of making a request. Here's my following test. I'm also wondering if there are better ways to refactor this

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");
        }
    }
}
lost9123193
  • 10,460
  • 26
  • 73
  • 113
  • Take a look of this - https://stackoverflow.com/questions/13918281/why-cant-i-change-the-return-value-in-a-rhino-mocks-stub-object or https://stackoverflow.com/questions/10720908/rhinomocks-mocking-a-method-whose-return-value-changes-even-when-passed-the-s – Kiran Thokal Jun 22 '20 at 20:05

2 Answers2

3

In this situations you can use test doubles. First , you should make your codes more testable ( Breaking dependency )

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);
        }
    }
}

And then you should create a stub to replace your real dependency. ( Also you can use frameworks like NSubstitute,Mock,...)

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

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

And finally

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");
        }
}
0

You can have another method in your class like the one below which returns an XmlDocument.

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

      return doc;
   }
Kara Kartal
  • 249
  • 1
  • 2
  • 8