2017-01-25 23 views
5

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(); 
     } 
    } 
} 
+0

'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

Trả lời

1

ClusterMonitoring đang được tạo trong phương pháp này bằng tay.

ClusterMonitoring monitoring = new ClusterMonitoring(); 

Điều này có nghĩa là không thể thay thế được. bạn cần phải tiêm ClusterMonitoring làm phụ thuộc vào bộ điều khiển để có khả năng thay thế nó khi thử nghiệm.

Đầu tiên trừu tượng ClusterMonitoring đằng sau một giao diện

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

và có nó kế thừa giao diện mà

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

Cập nhật điều khiển để có sự phụ thuộc qua 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); 
    } 
} 

Bây giờ cập nhật thử nghiệm để tiêm phụ thuộc vào bộ điều khiển theo thử nghiệm

[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); 
    } 
} 
Các vấn đề liên quan