如何使用NSubstitue框架从Controller类模拟基本方法

Kum*_*j K 5 c# testing unit-testing mocking nsubstitute

当Controller类中的Action方法调用它时,我需要一个基类中存在的mock方法.

这是我的Controller类,action方法Index()调用base方法GetNameNodeStatus().现在,GetNameNodeStatus()当action方法Index使用Nsubstitute 模拟框架调用它时,如何模拟基类中的当前内容.

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();
        }
     }
}
Run Code Online (Sandbox Code Playgroud)

这是我的基类Clustermonitoring

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

这是我的Test课程

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Nko*_*osi 1

ClusterMonitoring正在该方法中手动创建。

ClusterMonitoring monitoring = new ClusterMonitoring();
Run Code Online (Sandbox Code Playgroud)

这意味着替代它是不可能的。您需要将其ClusterMonitoring作为依赖项注入到控制器中,以便能够在测试时替换它。

ClusterMonitoring接口背后的第一个抽象

public interface IClusterMonitoring  {
    string GetNameNodeStatus(string hostName, Credential credential);
}
Run Code Online (Sandbox Code Playgroud)

并让它继承该接口

public class ClusterMonitoring : IClusterMonitoring {
    public virtual string GetNameNodeStatus(string hostName, Credential credential) { ... }
}
Run Code Online (Sandbox Code Playgroud)

更新控制器以通过构造函数获取依赖关系

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);
    }
 }
Run Code Online (Sandbox Code Playgroud)

现在更新测试以将依赖项注入到被测控制器中

[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); 
    }
}
Run Code Online (Sandbox Code Playgroud)