Last active
July 12, 2016 16:44
-
-
Save ivaneftimov/6180fb91e6ac3e43f4a5e431d89a70c7 to your computer and use it in GitHub Desktop.
Unit testing - Service locator 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.Abstractions; | |
| 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 string Date { get; set; } | |
| public string Message { get; set; } | |
| public string BreakingNewsMessage { get; set; } | |
| public string SelectedItem { get; set; } | |
| public ActionResult Index() | |
| { | |
| NewsManager newsManager = ObjectFactory.Resolve<NewsManager>("OpenAccessDataProvider".ToUpperInvariant()); | |
| var firstNewsItem = newsManager.GetNewsItems().FirstOrDefault(); | |
| BreakingNewsModel model = new BreakingNewsModel(); | |
| model.Message = firstNewsItem.Title; | |
| return View(model); | |
| } | |
| } | |
| } |
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\BreakingNewsControllerTests.cs | |
| using Microsoft.VisualStudio.TestTools.UnitTesting; | |
| using SitefinityWebApp.Mvc.Controllers; | |
| using SitefinityWebApp.Mvc.Models; | |
| using System.Linq; | |
| using System.Web.Mvc; | |
| using Telerik.Microsoft.Practices.Unity; | |
| using Telerik.Sitefinity.Abstractions; | |
| using Telerik.Sitefinity.Modules.News; | |
| namespace SitefinityWebApp.Tests | |
| { | |
| [TestClass] | |
| public class BreakingNewsControllerTests | |
| { | |
| [TestMethod] | |
| public void IndexAction_CorrectlySetsTitle_ToModel() | |
| { | |
| // Creating an empty unity container | |
| UnityContainer mockedContainer = new UnityContainer(); | |
| // Registering mocked implementation of NewsManager | |
| mockedContainer.RegisterType<NewsManager, MyNewsManager>(MyNewsManager.ProviderName.ToUpperInvariant()); | |
| // Run the controller with the empty container we just created | |
| ObjectFactory.RunWithContainer(mockedContainer, () => | |
| { | |
| BreakingNewsController controller = new BreakingNewsController(); | |
| ViewResult result = controller.Index() as ViewResult; | |
| BreakingNewsModel breakingNewsModel = result.Model as BreakingNewsModel; | |
| var manager = ObjectFactory.Resolve<NewsManager>(MyNewsManager.ProviderName.ToUpperInvariant()); | |
| string expectedFirstItemTitle = manager.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\Tests\Mocks\MyNewsItem.cs | |
| using Telerik.Sitefinity.Model; | |
| using Telerik.Sitefinity.News.Model; | |
| namespace SitefinityWebApp.Tests.Mocks | |
| { | |
| public class MyNewsItem : NewsItem | |
| { | |
| public override Lstring Title | |
| { | |
| get { return this.title; } | |
| set { this.title = value; } | |
| } | |
| private string title; | |
| } | |
| } |
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\MyNewsManager.cs | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using Telerik.Sitefinity.Modules.News; | |
| using Telerik.Sitefinity.News.Model; | |
| namespace SitefinityWebApp.Tests.Mocks | |
| { | |
| public class MyNewsManager : NewsManager | |
| { | |
| public MyNewsManager() : base(MyNewsManager.ProviderName) | |
| { | |
| // do nothing | |
| } | |
| protected override void Initialize() | |
| { | |
| // do nothing | |
| } | |
| protected override void SetProvider(string providerName, string transactionName) | |
| { | |
| // do nothing | |
| } | |
| public override IQueryable<NewsItem> GetNewsItems() | |
| { | |
| return new List<NewsItem>() | |
| { | |
| new MyNewsItem() { Title = "Test: First news" }, | |
| new MyNewsItem() { Title = "Test: Second news" }, | |
| new MyNewsItem() { Title = "Test: Third news" } | |
| }.AsQueryable(); | |
| } | |
| public const string ProviderName = "OpenAccessDataProvider"; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment