mercredi 25 janvier 2017

How do I mock a base method in the Controller class using the NSubstitue framework

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

Here is my Controller class below, the action method Index() calls the base method GetNameNodeStatus(). Now how can I mock the GetNameNodeStatus() present in the base class when the action method Index calls it using the Nsubstitute mocking 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();
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire