0

我是Sitecore的開發人員,我想測試如下指數確切創建示例Sitecore的螺旋單元測試項目()我們的「HomeBottomContentController的操作方法「控制器,沒有任何依賴注入到一個構造函數。請注意,註釋掉的代碼正是我不想做的。如何單元測試GlassController行動沒有SitecoreContext依賴注入

public class HomeBottomContentController : GlassController 
{ 
    // I want to test the EXACT method below 
    public override ActionResult Index() 
    { 
     var context = new SitecoreContext(); 
     var model = context.GetCurrentItem<Home_Control>(); 
     return View("~/Views/HomeBottomContent/HomeBottomContent.cshtml", model); 
    } 

    /* 
    // I do NOT want to have any of the below code for injecting ISitecoreContext into a constructor and testing the IndexTest() below it. 
    private readonly ISitecoreContext _iSitecoreContext; 
    public HomeBottomContentController(ISitecoreContext iSitecoreContext) 
    { 
     _iSitecoreContext = iSitecoreContext; 
    } 

    public HomeBottomContentController() 
    { } 

    public ActionResult IndexTest() 
    { 
     var model = _iSitecoreContext.GetCurrentItem<Home_Control>(); 
     return View("~/Views/HomeBottomContent/HomeBottomContent.cshtml", model); 
    } 
    */ 
} 

這就是我在單元測試課上所擁有的。再次,我已經註釋掉正是我想做的事:

[TestClass] 
public class MvcUnitTests 
{ 
    [TestMethod] 
    public void Test_HomeBottomContentController_With_ISitecoreContext() 
    { 
     /* 
     // I don't want to do below... 
     var model = new Home_Control() 
     { Bottom_Content = "XYZ" }; 

     var iSitecoreContext = new Mock<Glass.Mapper.Sc.ISitecoreContext>(); 
     iSitecoreContext.Setup(_ => _.GetCurrentItem<Home_Control>(false, false)).Returns(model); 

     HomeBottomContentController controllerUnderTest = new HomeBottomContentController(iSitecoreContext.Object); 

     var result = controllerUnderTest.IndexTest() as ViewResult; 
     */ 

     //I want to test using the exact constructor below and calling that exact Index() method. 
     HomeBottomContentController controllerUnderTest = new HomeBottomContentController(); 
     var result = controllerUnderTest.Index() as ViewResult; 
     Assert.IsNotNull(result); 
     Assert.IsNotNull(result.Model); 
     //Assert.AreEqual(((Home_Control)result.Model).Bottom_Content, "XYZ"); 
    } 
} 

我如何測試我控制器的確切Index()方法,而無需添加代碼,HomeBottomContentController類,使依賴注入轉換爲構造函數(如上面的註釋代碼)?我不想將代碼添加到HomeBottomContentController()。

@Aleksey Shevchenko如果我嘗試你的解決方案,我該如何將模型連接到iSitecoreContext,然後將其分配給controllerUnderTest.FakeContext?我下面的代碼引發編譯錯誤(您不能從Glass.Mapper.Sc.ISitecoreContext的模擬轉換成Glass.Mapper.Sc.ISitecoreContext,我們如何實現這一目標):

var model = new Home_Control() 
{ Top_Content = "Some Dummy Test Home Top Content" }; 
var iSitecoreContext = new Mock<Glass.Mapper.Sc.ISitecoreContext>(); 
//var iSitecoreContext = new Glass.Mapper.Sc.SitecoreContext();  
iSitecoreContext.Setup(_ => _.GetCurrentItem<Home_Control>(false, false)).Returns(model); 
FakeHomeTopContentController controllerUnderTest = new FakeHomeTopContentController(); 
controllerUnderTest.FakeContext = (Glass.Mapper.Sc.SitecoreContext)iSitecoreContext; 

回答

2

如果你不想通過構造函數創建依賴注入,你可以通過受保護的虛擬方法來實現。類似的東西:

public class HomeBottomContentController : GlassController 
{ 
    public override ActionResult Index() 
    { 
     var context = this.GetContext(); 
     var model = context.GetCurrentItem<Home_Control>(); 
     return View("~/Views/HomeBottomContent/HomeBottomContent.cshtml", model); 
    } 

    protected virtual SitecoreContext GetContext() 
    { 
     return new SitecoreContext(); 
    } 
} 

[TestClass] 
public class MvcUnitTests 
{ 
    [TestMethod] 
    public void Test_HomeBottomContentController_With_ISitecoreContext() 
    {  
     var controllerUnderTest = new FakeHomeBottomContentController(); 
     controllerUnderTest.FakeContext = /* set what you want */; 
     var result = controllerUnderTest.Index() as ViewResult; 
     Assert.IsNotNull(result); 
     Assert.IsNotNull(result.Model);   
    } 

    public class FakeHomeBottomContentController : HomeBottomContentController 
    { 
     public SitecoreContext FakeContext { get; set; } 

     protected override SitecoreContext GetContext() 
     { 
      return this.FakeContext; 
     } 
    } 
} 
+0

謝謝!這裏是我有什麼,它給出了錯誤,我怎樣才能將模型掛接到iSitecoreContext 並將其設置爲FakeContext?: var model = new Home_Control() {Top_Content =「Some Dummy Test Home Top Content」 }; var iSitecoreContext = new Mock (); iSitecoreContext.Setup(_ => _.GetCurrentItem (false,false))。 FakeHomeTopContentController controllerUnderTest = new FakeHomeTopContentController(); controllerUnderTest.FakeContext =(Glass.Mapper.Sc.SitecoreContext)iSitecoreContext; – user3034243

+0

@ user3034243嘗試將最後一行更改爲此類視圖: controllerUnderTest.FakeContext =(Glass.Mapper.Sc.SitecoreContext)(iSitecoreContext.Object); –

0

我不知道你的結束源代碼的能力限制,但基於對對方的回答您的評論您可以訪問它。我會選擇窮人的DI,在控制器上有兩個構造函數。單元測試使用第二個構造函數,而MVC將使用無參數構造函數。

public class HomeBottomContentController : GlassController 
{ 

ISitecoreContext _iSitecoreContext; 

public HomeBottomContentController() :this(new SitecoreContext()){ 
} 


public HomeBottomContentController(ISitecoreContext iSitecoreContext) 
{ 
    _iSitecoreContext = iSitecoreContext; 
} 



public ActionResult IndexTest() 
{ 
    var model = _iSitecoreContext.GetCurrentItem<Home_Control>(); 
    return View("~/Views/HomeBottomContent/HomeBottomContent.cshtml", model); 
} 

} 

[TestClass] 
public class MvcUnitTests 
{ 
[TestMethod] 
public void Test_HomeBottomContentController_With_ISitecoreContext() 
{ 

    var model = new Home_Control() 
    { Bottom_Content = "XYZ" }; 

    var iSitecoreContext = new Mock<Glass.Mapper.Sc.ISitecoreContext>(); 
    iSitecoreContext.Setup(_ => _.GetCurrentItem<Home_Control>(false, false)).Returns(model); 

    HomeBottomContentController controllerUnderTest = new HomeBottomContentController(iSitecoreContext.Object); 

    var result = controllerUnderTest.IndexTest() as ViewResult; 

}