Last active
July 12, 2016 17:05
-
-
Save ivaneftimov/dd93bb043f1ce853a2f88dbc64ce474c to your computer and use it in GitHub Desktop.
Unit testing - Dependency injection pattern | proper manager use
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SitefinityWebapp\Mvc\Controllers\BreakingNewsController.cs | |
| using SitefinityWebApp.Mvc.Models; | |
| using SitefinityWebApp.Wrappers; | |
| using System.Linq; | |
| using System.Web.Mvc; | |
| using Telerik.Sitefinity.Modules.News; | |
| using Telerik.Sitefinity.Mvc; | |
| namespace SitefinityWebApp.Mvc.Controllers | |
| { | |
| [ControllerToolboxItem(Title = "Breaking News", Name = "BreakingNews", SectionName = "CustomMvcWidgets")] | |
| public class BreakingNewsController : Controller | |
| { | |
| public BreakingNewsController(INewsManagerWrapper newsManagerWrapper) | |
| { | |
| this.newsManager = newsManagerWrapper.GetManager(); | |
| } | |
| public string Date { get; set; } | |
| public string Message { get; set; } | |
| public string BreakingNewsMessage { get; set; } | |
| public string SelectedItem { get; set; } | |
| public ActionResult Index() | |
| { | |
| var firstNewsItem = this.newsManager.GetNewsItems().FirstOrDefault(); | |
| BreakingNewsModel model = new BreakingNewsModel(); | |
| model.Message = firstNewsItem.Title; | |
| return View(model); | |
| } | |
| private NewsManager newsManager; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SitefinityWebApp\Tests\BreakingNewsControllerDITests.cs | |
| using Microsoft.VisualStudio.TestTools.UnitTesting; | |
| using SitefinityWebApp.Mvc.Controllers; | |
| using SitefinityWebApp.Mvc.Models; | |
| using SitefinityWebApp.Tests.Mocks; | |
| using System.Linq; | |
| using System.Web.Mvc; | |
| namespace SitefinityWebApp.Tests | |
| { | |
| [TestClass] | |
| public class BreakingNewsControllerDITests | |
| { | |
| [TestMethod] | |
| public void IndexAction_CorrectlySetsTitle_ToModel() | |
| { | |
| // Pass my mocked instance of a news manager to the controller. | |
| var myNewsManagerWrapper = new MyNewsManagerWrapper(); | |
| BreakingNewsController controller = new BreakingNewsController(myNewsManagerWrapper); | |
| ViewResult result = controller.Index() as ViewResult; | |
| BreakingNewsModel breakingNewsModel = result.Model as BreakingNewsModel; | |
| string expectedFirstItemTitle = myNewsManagerWrapper.GetManager().GetNewsItems().First().Title; | |
| Assert.AreEqual<string>(expectedFirstItemTitle, breakingNewsModel.Message); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SitefinityWebApp\Wrappers\INewsManagerWrapper.cs | |
| using Telerik.Sitefinity.Modules.News; | |
| namespace SitefinityWebApp.Wrappers | |
| { | |
| public interface INewsManagerWrapper | |
| { | |
| NewsManager GetManager(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SitefinityWebApp\Tests\Mocks\MyNewsManagerWrapper.cs | |
| using SitefinityWebApp.Tests.BreakingNews.Mocks; | |
| using SitefinityWebApp.Wrappers; | |
| using Telerik.Sitefinity.Modules.News; | |
| namespace SitefinityWebApp.Tests.Mocks | |
| { | |
| public class MyNewsManagerWrapper : INewsManagerWrapper | |
| { | |
| public NewsManager GetManager() | |
| { | |
| return new MyNewsManager(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SitefinityWebApp\Wrappers\NewsManagerWrapper.cs | |
| using Telerik.Sitefinity.Modules.News; | |
| namespace SitefinityWebApp.Wrappers | |
| { | |
| public class NewsManagerWrapper : INewsManagerWrapper | |
| { | |
| public NewsManager GetManager() | |
| { | |
| return NewsManager.GetManager(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SitefinityWebApp\DI\NinjectControllerFactory.cs | |
| using Ninject; | |
| using SitefinityWebApp.Wrappers; | |
| using System; | |
| using System.Web.Mvc; | |
| using System.Web.Routing; | |
| using Telerik.Sitefinity.Frontend; | |
| using Telerik.Sitefinity.Frontend.Mvc.Infrastructure.Controllers; | |
| namespace SitefinityWebApp.DI | |
| { | |
| public class NinjectControllerFactory : FrontendControllerFactory | |
| { | |
| public NinjectControllerFactory() | |
| { | |
| this.ninjectKernel = FrontendModule.Current.DependencyResolver; | |
| // Add dependency bindings here | |
| this.ninjectKernel.Bind<INewsManagerWrapper>().To<NewsManagerWrapper>(); | |
| } | |
| protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) | |
| { | |
| if (controllerType == null) | |
| { | |
| return null; | |
| } | |
| var resolvedController = this.ninjectKernel.Get(controllerType); | |
| IController controller = resolvedController as IController; | |
| return controller; | |
| } | |
| private IKernel ninjectKernel; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment