首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在没有GlassController依赖注入的情况下对SitecoreContext操作进行单元测试

如何在没有GlassController依赖注入的情况下对SitecoreContext操作进行单元测试
EN

Stack Overflow用户
提问于 2017-09-25 15:53:25
回答 2查看 596关注 0票数 0

我是一名sitecore开发人员,我想创建一个示例sitecore螺旋单元测试项目,用于测试我们的“”控制器的索引()下面的操作方法,而不需要在构造函数中注入任何依赖项。请注意,注释掉的代码正是我不想做的。

代码语言:javascript
运行
复制
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);
    }
    */
}

这是我在单元测试课上的内容。同样,我所评论的正是我不想做的事情:

代码语言:javascript
运行
复制
[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");
    }
}

如何测试我的控制器的确切索引()方法,而不必向HomeBottomContentController类中添加代码,从而使依赖项注入到构造函数中(如上面的注释代码)?我不想向HomeBottomContentController()添加代码。

@Aleksey如果我尝试您的解决方案,如何将模型连接到iSitecoreContext,然后将其分配给controllerUnderTest.FakeContext?下面的代码会引发编译错误(您不能从Glass.Mapper.Sc.ISitecoreContext模式转换为Glass.Mapper.Sc.ISitecoreContext,我们如何实现这一点):

代码语言:javascript
运行
复制
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;
EN

回答 2

Stack Overflow用户

发布于 2017-09-26 09:14:08

如果不希望通过构造函数创建依赖项注入,则可以通过受保护的虚拟方法来实现。就像这样:

代码语言:javascript
运行
复制
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;
        }
    }   
}
票数 2
EN

Stack Overflow用户

发布于 2018-02-22 19:10:53

我不知道你结束源代码的能力有多大的限制,但是根据你对另一个答案的评论,你可以访问它。我会选择穷人的DI,在控制器上有两个构造函数。单元测试使用第二个构造函数,MVC将使用无参数构造函数。

代码语言:javascript
运行
复制
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;

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46409352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档