Skip to content

Instantly share code, notes, and snippets.

@dsolovay dsolovay/AutofacConfig.cs
Last active Aug 29, 2015

Embed
What would you like to do?
Sitecore TDD with Glass
using System;
using System.Reflection;
using System.Web.Mvc;
using Autofac;
using Autofac.Integration.Mvc;
using Glass.Mapper.Sc;
namespace AutofacDemo
{
public class AutofacConfig
{
public static void RegisterAutofac()
{
var builder = new ContainerBuilder();
// Register MVC controllers.
builder.RegisterControllers(Assembly.GetExecutingAssembly());
// Register repositories
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.Where(t => t.Name.ToLower().EndsWith("repository"))
.AsImplementedInterfaces();
// Register Glass SitecoreContext
builder.RegisterType<SitecoreContext>().As<ISitecoreContext>();
// Set the dependency resolver to be Autofac.
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
}
<%@ Application Language='C#' Inherits="Sitecore.Web.Application" %>
<%@ Import Namespace="System.Web.Optimization" %>
<%@ Import Namespace="AutofacDemo" %>
<%@ Import Namespace="GlassDemo" %>
<script Language="c#" RunAt="server">
public void Application_Start()
{
BundleConfig.RegisterBundles(BundleTable.Bundles);
AutofacConfig.RegisterAutofac();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
}
</script>
using System.Collections.Generic;
namespace GlassDemo.Models
{
public interface ISampleItem
{
IEnumerable<ISampleItem> Children { get; set; }
string Name { get; set; }
string Url { get; set; }
}
}
@using GlassDemo.Models
@model GlassDemo.Models.NavBarModel
@* Bootstrap Navbar*@
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">Home</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
@foreach (NavItem navItem in Model.Items)
{
<li class=@(navItem.Active? "active" : "")>
<a href="@navItem.Href">@navItem.Name</a>
</li>
}
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Glass.Mapper.Sc;
using GlassDemo.Models;
namespace GlassDemo.Controllers
{
public class NavbarController : Controller
{
private readonly ISitecoreContext _sitecoreContext;
public NavbarController(ISitecoreContext sitecoreContext)
{
_sitecoreContext = sitecoreContext;
}
// GET: Navbar
public ActionResult Index()
{
var navBarModel = new NavBarModel();
var home = _sitecoreContext.GetHomeItem<ISampleItem>();
var currentUrl = _sitecoreContext.GetCurrentItem<ISampleItem>().Url;
foreach (var child in home.Children)
{
navBarModel.Items.Add(new NavItem() {
Name = child.Name,
Href = child.Url,
Active = GetActiveStatus(child, currentUrl)});
}
return View("NavBar", navBarModel);
}
private static bool GetActiveStatus(ISampleItem child, string currentUrl)
{
return currentUrl.Contains(child.Url);
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Web.Mvc;
using Glass.Mapper.Sc;
using GlassDemo.Controllers;
using GlassDemo.Models;
using NUnit.Framework;
using NSubstitute;
using Sitecore.Web.UI.XslControls;
namespace GlassDemo.Tests
{
public class NavbarControllerTests
{
private ISitecoreContext _sitecoreContext;
private NavbarController _controller;
[SetUp]
public void SetUp()
{
_sitecoreContext = Substitute.For<ISitecoreContext>();
_controller = new NavbarController(_sitecoreContext);
}
[Test]
public void NavbarController_WhenIndexCalled_ReturnsNavbarView()
{
// arrange
// act
ViewResult viewResult = _controller.Index() as ViewResult;
// assert
Assert.AreEqual("NavBar", viewResult.ViewName);
}
[Test]
public void NavbarController_WhenIndexCalled_ReturnsNavbarModel()
{
// arrange
// act
ViewResult viewResult = _controller.Index() as ViewResult;
// assert
NavBarModel model = viewResult.Model as NavBarModel;
Assert.NotNull(model);
}
[Test]
public void NavbarController_WhenNoChildrenOfHome_ReturnsEmptyList()
{
// arrange
_sitecoreContext.GetHomeItem<ISampleItem>().Children.Returns(new List<ISampleItem>());
// act
ViewResult viewResult = _controller.Index() as ViewResult;
// assert
NavBarModel model = viewResult.Model as NavBarModel;
Assert.AreEqual(0, model.Items.Count);
}
[Test]
public void NavbarController_WhenHomeHasChildren_ReturnsThem()
{
// arrange
var child1 = Substitute.For<ISampleItem>();
var child2 = Substitute.For<ISampleItem>();
_sitecoreContext.GetHomeItem<ISampleItem>().Children.Returns(new List<ISampleItem>()
{
child1, child2
});
// act
ViewResult viewResult = _controller.Index() as ViewResult;
// assert
NavBarModel model = viewResult.Model as NavBarModel;
Assert.AreEqual(2, model.Items.Count);
}
[Test]
public void NavbarController_WhenChildReturned_HasName()
{
// arrange
var child1 = Substitute.For<ISampleItem>();
child1.Name = "Some Name";
_sitecoreContext.GetHomeItem<ISampleItem>().Children.Returns(new List<ISampleItem>()
{
child1
});
// act
ViewResult viewResult = _controller.Index() as ViewResult;
// assert
NavBarModel model = viewResult.Model as NavBarModel;
var child = model.Items[0];
Assert.AreEqual("Some Name", child.Name);
}
[Test]
public void NavbarController_WhenChildReturned_HasUrl()
{
// arrange
var child1 = Substitute.For<ISampleItem>();
child1.Url = "/someurl";
_sitecoreContext.GetHomeItem<ISampleItem>().Children.Returns(new List<ISampleItem>()
{
child1
});
// act
ViewResult viewResult = _controller.Index() as ViewResult;
// assert
NavBarModel model = viewResult.Model as NavBarModel;
var child = model.Items[0];
Assert.AreEqual("/someurl", child.Href);
}
[Test]
public void NavbarController_WhenChildIsContextItem_SetActive()
{
// arrange
var child1 = Substitute.For<ISampleItem>();
var child2 = Substitute.For<ISampleItem>();
child1.Url = "/child1";
child2.Url = "/child2";
_sitecoreContext.GetCurrentItem<ISampleItem>().Url.Returns("/child1");
_sitecoreContext.GetHomeItem<ISampleItem>().Children.Returns(new List<ISampleItem>()
{
child1,child2
});
// act
ViewResult viewResult = _controller.Index() as ViewResult;
// assert
NavBarModel model = viewResult.Model as NavBarModel;
Assert.AreEqual(true, model.Items[0].Active);
Assert.AreEqual(false, model.Items[1].Active);
}
[Test]
public void NavbarController_WhenNavBarItemIsAncestorOfContextItem_SetActive()
{
// arrange
var child1 = Substitute.For<ISampleItem>();
var child2 = Substitute.For<ISampleItem>();
child1.Url = "/child1";
child2.Url = "/child2";
_sitecoreContext.GetCurrentItem<ISampleItem>().Url.Returns("/child1/grandchild");
_sitecoreContext.GetHomeItem<ISampleItem>().Children.Returns(new List<ISampleItem>()
{
child1,child2
});
// act
ViewResult viewResult = _controller.Index() as ViewResult;
// assert
NavBarModel model = viewResult.Model as NavBarModel;
Assert.AreEqual(true, model.Items[0].Active);
Assert.AreEqual(false, model.Items[1].Active);
}
}
}
using System.Collections.Generic;
namespace GlassDemo.Models
{
public class NavBarModel
{
public NavBarModel()
{
Items = new List<NavItem>();
}
public List<NavItem> Items { get; set; }
}
public class NavItem
{
public bool Active { get; set; }
public string Href { get; set; }
public string Name { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.