我是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;
謝謝!這裏是我有什麼,它給出了錯誤,我怎樣才能將模型掛接到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
@ user3034243嘗試將最後一行更改爲此類視圖: controllerUnderTest.FakeContext =(Glass.Mapper.Sc.SitecoreContext)(iSitecoreContext.Object); –