How I mock the base method from the Controller class using the NSubstitue framework

I need a mock method present in the base class when the Action method in the Controller class calls it.

Here is my controller class below, the Index() action method calls the GetNameNodeStatus() base method. Now, how can I mock GetNameNodeStatus() present in the base class when the Index action method calls it using fake Nsubstitute frameworks.

 using Cluster.Manager.Helper; using Cluster.Manager.Messages; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; namespace Cluster.Manager { public class HomeController : Controller { // GET: Home public ActionResult Index() { ClusterMonitoring monitoring = new ClusterMonitoring(); string getStatus = monitoring.GetNameNodeStatus("", new Credential()); return View(); } } } 

Here is my base class Clustermonitoring

 namespace Cluster.Manager.Helper { public class ClusterMonitoring { public virtual string GetNameNodeStatus(string hostName, Credential credential) { return "Method Not Implemented"; } } } 

And here is my test class

 namespace NSubstituteControllerSupport { [TestFixture] public class UnitTest1 { [Test] public void ValidateNameNodeStatus() { var validation = Substitute.ForPartsOf<ClusterMonitoring>(); validation.When(actionMethod => actionMethod.GetNameNodeStatus(Arg.Any<string>(), Arg.Any<Credential>())).DoNotCallBase(); validation.GetNameNodeStatus("ipaddress", new Credential()).Returns("active"); var controllers = Substitute.For<HomeController>(); controllers.Index(); } } } 
+5
source share
1 answer

ClusterMonitoring is created manually.

 ClusterMonitoring monitoring = new ClusterMonitoring(); 

This means that its substitution is impossible. you need to introduce ClusterMonitoring as a controller dependency in order to be able to replace it during testing.

The first ClusterMonitoring abstraction behind the interface

 public interface IClusterMonitoring { string GetNameNodeStatus(string hostName, Credential credential); } 

and inherit this interface

 public class ClusterMonitoring : IClusterMonitoring { public virtual string GetNameNodeStatus(string hostName, Credential credential) { ... } } 

Update the controller to accept the dependency through the constructor

 public class HomeController : Controller { private readonly IClusterMonitoring monitoring; public HomeController(IClusterMonitoring monitoring) { this.monitoring = monitoring; } // GET: Home public ActionResult Index() { var status = monitoring.GetNameNodeStatus("", new Credential()); return View(status); } } 

Now update the test to add dependency to the controller under test

 [TestFixture] public class UnitTest1 { [Test] public void ValidateNameNodeStatus() { //Arrange var expected = "active"; var validation = Substitute.For<IClusterMonitoring>(); validation.GetNameNodeStatus("", new Credential()).Returns(expected); var controller = new HomeController(validation); //Act var actual = controllers.Index() as ViewResult; //Assert Assert.IsNotNull(actual); Assert.AreEqual(expected, actual.Model); } } 
+1
source

Source: https://habr.com/ru/post/1263317/


All Articles