How to Mock another method of same class we are testing in c# without dependency injection

I am facing one scenario where I need to mock another method of the same class in c#. But I am facing issue to mock another method and the same I am sharing below.

 Interface --> 

  public interface IFetch
  {
     string GetColorCodes();

     string FetchData();

  }

  Class-->
 public class Fetch : IFetch 
 {

    public Fetch()
    {
     //I dont have any dependency injection
    }

   public string GetColorCodes() {
    return "Yellow";
   }

   public string FetchData(){
   string  color= GetColorCodes();
   return color;
 }

Now, while creating a unit test for testing FetchData() method I don't want it should internally call GetColorCodes() method and returns "Yellow". I want to return some other color like "Red". Since there is no dependency and method is in the same class I am not able to find out the way. If anyone has an idea please share with this sample code

[TestFixture]
class FetchTest 
{
   [Test]
   public void FetchDataTest()
  {
   Fetch obj=new Fetch();
   string color= obj.FetchData();
  Assert.IsEquals(color,"Red")
  }

}

I want to mock the GetColorCodes() method from the test to Return Red color.

2 answers

  • answered 2019-11-14 05:18 Michael Kokorin

    FetchData method doesn't have any dependencies here. You can't mock part of class to test another part of class. You need to introduce some dependency here, e.g. like this:

       public interface IColorProvider
       {
           string GetColor();
       }
    
       public class DefaultColorProvider : IColorProvider
       {
           public string GetColor => "Yellow";
       }
    
        public class Fetch : IFetch 
        {
            private readonly IColorProvider _colorProvider;
            public Fetch(IColorProvider colorProvider)
            {
              _colorProvider = colorProvider
            }
    
            public string FetchData(){
            string  color= _colorProvider.GetColor()
            return color;
        }       
    

    In this case - you have clear dependency and you can mock behavior of IColorProvider and test behavior of FetchData.

    UPDATE: You can do something like this:

        public class Fetch : IFetch 
        {
            private readonly IColorProvider _colorProvider;
            public Fetch(IColorProvider colorProvider)
            {
              _colorProvider = colorProvider
            }
    
            public string GetColorCodes() {
                 return _colorProvider.GetColor();
            }
    
            public string FetchData(){
            string  color= GetColorCodes();
            return color;
        }
    

    So changes in the class will be very small but it will give you ability to test this.

  • answered 2019-11-14 05:36 CRice

    If you have no problems with making GetColorCodes() virtual then you can use the Extract and Override test pattern. In your test, new up FetchStub with the mock value you wish to use and call your method under test FetchData from the same instance:

    public class FetchStub : Fetch
    {
        private readonly string _colorCode;
        public FetchStub(string colorCode)
        {
            _colorCode = colorCode;     
        }
    
        public override string GetColorCodes()
        {
            return _colorCode;
        }
    }
    
    
    public class Fetch
    {
    
        public Fetch()
        {
            //I dont have any dependency injection
        }
    
        public virtual string GetColorCodes()
        {
            return "Yellow";
        }
    
        public string FetchData()
        {
            string color = GetColorCodes();
            return color;
        }
    }