Last active
October 25, 2019 22:18
-
-
Save ivaneftimov/1a4018e4396d856eadc71402c5a76d5b to your computer and use it in GitHub Desktop.
Unit testing - Dependency injection pattern
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 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(NewsManager newsManager) | |
| { | |
| this.newsManager = newsManager; | |
| } | |
| 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 myNewsManager = new MyNewsManager(); | |
| BreakingNewsController controller = new BreakingNewsController(myNewsManager); | |
| ViewResult result = controller.Index() as ViewResult; | |
| BreakingNewsModel breakingNewsModel = result.Model as BreakingNewsModel; | |
| string expectedFirstItemTitle = myNewsManager.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
| // Global.asax.cs | |
| using SitefinityWebApp.DI; | |
| using System; | |
| using System.Web; | |
| using System.Web.Mvc; | |
| using Telerik.Microsoft.Practices.Unity; | |
| using Telerik.Sitefinity.Abstractions; | |
| using Telerik.Sitefinity.Mvc; | |
| namespace SitefinityWebApp | |
| { | |
| public class Global : HttpApplication | |
| { | |
| protected void Application_Start(object sender, EventArgs e) | |
| { | |
| Bootstrapper.Bootstrapped += this.Bootstrapper_Bootstrapped; | |
| } | |
| private void Bootstrapper_Bootstrapped(object sender, EventArgs e) | |
| { | |
| // Use Service Locator mechanism to register our NinjectController factory which will provide DI for controller dependencies. | |
| ObjectFactory.Container.RegisterType<ISitefinityControllerFactory, NinjectControllerFactory>(new ContainerControlledLifetimeManager()); | |
| var factory = ObjectFactory.Resolve<ISitefinityControllerFactory>(); | |
| // Set our factory as a default controller factory | |
| ControllerBuilder.Current.SetControllerFactory(factory); | |
| } | |
| } | |
| } |
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 System; | |
| using System.Web.Mvc; | |
| using System.Web.Routing; | |
| using Telerik.Sitefinity.Frontend; | |
| using Telerik.Sitefinity.Frontend.Mvc.Infrastructure.Controllers; | |
| using Telerik.Sitefinity.Modules.News; | |
| namespace SitefinityWebApp.DI | |
| { | |
| public class NinjectControllerFactory : FrontendControllerFactory | |
| { | |
| public NinjectControllerFactory() | |
| { | |
| this.ninjectKernel = FrontendModule.Current.DependencyResolver; | |
| // Add dependency bindings here | |
| this.ninjectKernel.Bind<NewsManager>().To<NewsManager>(); | |
| } | |
| 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