Tôi cần một phương thức giả hiện diện trong một lớp cơ sở khi một phương thức Action trong lớp Controller gọi nó.Làm thế nào để giả lập một phương thức cơ bản từ lớp Controller bằng cách sử dụng khung NSubstitue
Đây là lớp Bộ điều khiển bên dưới, phương thức hành động Index()
gọi phương thức cơ bản GetNameNodeStatus()
. Bây giờ làm thế nào tôi có thể mô phỏng hiện tại GetNameNodeStatus()
trong lớp cơ sở khi phương thức hành động Index
gọi nó bằng cách sử dụng khung làm việc giả lập 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();
}
}
}
Đây là cơ sở của tôi lớp Clustermonitoring
namespace Cluster.Manager.Helper
{
public class ClusterMonitoring
{
public virtual string GetNameNodeStatus(string hostName, Credential credential)
{
return "Method Not Implemented";
}
}
}
Và đây là class Test tôi
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();
}
}
}
'ClusterMonitoring' được tạo theo cách thủ công. Điều này có nghĩa là thay thế nó là không thể. bạn cần phải tiêm 'ClusterMonitoring' như một phụ thuộc vào bộ điều khiển. – Nkosi