Created
November 4, 2024 09:58
-
-
Save keyan1603/50111606e3a88d8d51862fc34cc9102f to your computer and use it in GitHub Desktop.
Sitecore unit test sample
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
using AutoFixture; | |
using AutoFixture.AutoNSubstitute; | |
using AutoFixture.Xunit2; | |
namespace Foundation.Tests.Extensions | |
{ | |
internal class AutoDbDataAttribute :AutoDataAttribute | |
{ | |
public AutoDbDataAttribute() | |
:base(() => new Fixture() | |
.Customize(new DatabaseCustomization()) | |
.Customize(new ItemCustomization()) | |
.Customize(new AutoNSubstituteCustomization())) | |
{ | |
} | |
} | |
} | |
using AutoFixture; | |
using NSubstitute; | |
using Sitecore.Data; | |
namespace Foundation.Tests.Extensions | |
{ | |
internal class DatabaseCustomization : AutoFixture.ICustomization | |
{ | |
public void Customize(IFixture fixture) | |
{ | |
fixture.Customize<Database>(x => x.FromFactory( | |
() => Substitute.For<Database>()).OmitAutoProperties()); | |
} | |
} | |
} | |
using AutoFixture; | |
using AutoFixture.Kernel; | |
using NSubstitute; | |
using Sitecore.Data; | |
using Sitecore.Data.Items; | |
namespace Foundation.Tests.Extensions | |
{ | |
internal class ItemCustomization : ICustomization | |
{ | |
public void Customize(IFixture fixture) | |
{ | |
fixture.Customize<Item>(x => | |
x.FromFactory(() => CreateItem(fixture)).OmitAutoProperties()); | |
} | |
private static Item CreateItem(ISpecimenBuilder fixture) | |
{ | |
var item = Substitute.For<Item>( | |
fixture.Create<ID>(), | |
fixture.Create<ItemData>(), | |
fixture.Create<Database>()); | |
return item; | |
} | |
} | |
} | |
using Sitecore.Data.Items; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
namespace Foundation.Helper.Common.GlassMapper.Controller | |
{ | |
public interface IGlassStandardController | |
{ | |
T GetRenderingParameters<T>() where T : class; | |
T GetContextItem<T>(bool isLazy = false, bool inferType = false) where T : class; | |
T GetDataSourceItem<T>(bool isLazy = false, bool inferType = false) where T : class; | |
Item DataSourceItem { get; } | |
} | |
} | |
Test class | |
using Feature.LocalFeature.Controllers; | |
using Foundation.Helper.Common.GlassMapper.Controller; | |
using NSubstitute; | |
using Sitecore.Data; | |
using Sitecore.Data.Items; | |
using Sitecore.Mvc.Common; | |
using Sitecore.Mvc.Presentation; | |
using System.Web.Mvc; | |
using Feature.LocalFeature.Tests.Extensions; | |
using Xunit; | |
using Feature.LocalFeature.Models.Interface; | |
namespace Feature.LocalFeature.Tests | |
{ | |
public class LocalFeatureControllerTests | |
{ | |
[Theory, AutoDbData] | |
public void LocalFeatureController_LocalFeatureCard(Database db, Item item, IGlassStandardController glassStandardController) | |
{ | |
//Arrange | |
var controller = new LocalFeatureController(glassStandardController); | |
//item["Title"].Returns("Welcome!"); | |
db.GetItem("/sitecore/content/home").Returns(item); | |
var context = new RenderingContext(); | |
context.Rendering = new Rendering(); | |
context.Rendering.Item = item; | |
context.Rendering.Parameters = new RenderingParameters("cssClass=orange&position=left"); | |
ContextService.Get().Push(context); | |
//Act | |
var res = controller.LocalFeatureCard(); | |
//Assert.Equal("Welcome!", item["Title"]); | |
//Assert | |
Assert.IsType<ViewResult>(res); | |
} | |
[Theory, AutoDbData] | |
public void Index_ReturnsCorrectViewName(IGlassStandardController glassStandardController) | |
{ | |
//Arrange | |
var controller = new LocalFeatureController(glassStandardController); | |
//Act | |
var result = controller.Index() as ViewResult; | |
// Assert | |
Assert.Equal(string.Empty, result?.ViewName); | |
} | |
[Theory, AutoDbData]// High Priority | |
public void LocalFeatureController_LocalFeatureCard_When_Param_Not_Null(Database db, Item item, IGlassStandardController glassStandardController) | |
{ | |
//Assert.NotNull(database); | |
var controller = new LocalFeatureController(glassStandardController); | |
//item["Title"].Returns("Welcome!"); | |
db.GetItem("/sitecore/content/home").Returns(item); | |
var context = new RenderingContext(); | |
context.Rendering = new Rendering(); | |
context.Rendering.Item = item; | |
context.Rendering.Parameters = new RenderingParameters("cssClass=orange&position=left"); | |
ContextService.Get().Push(context); | |
//Act | |
var result = controller.LocalFeatureCard(); | |
//Assert | |
var viewResult = result as ViewResult; | |
Assert.IsType<LocalFeature.Models.LocalFeature>(viewResult.Model); | |
} | |
[Theory, AutoDbData]// High Priority | |
public void LocalFeatureController_LocalFeatureCard_When_Param_null(Database db, Item item, IGlassStandardController glassStandardController) | |
{ | |
//Arrange | |
db.GetItem("/sitecore/content/home").Returns(item); | |
var context = new RenderingContext(); | |
context.Rendering = new Rendering(); | |
context.Rendering.Item = item; | |
context.Rendering.Parameters = new RenderingParameters("cssClass=orange&position=left"); | |
ContextService.Get().Push(context); | |
glassStandardController.GetRenderingParameters<ILocalFeatureParameter>().Returns((ILocalFeatureParameter)null); | |
glassStandardController.GetDataSourceItem<ILocalFeature>().Returns((ILocalFeature)null); | |
var controller = new LocalFeatureController(glassStandardController); | |
//Act | |
var result = controller.LocalFeatureCard(); | |
var viewResult = result as ViewResult; | |
LocalFeature.Models.LocalFeature ac = (LocalFeature.Models.LocalFeature)viewResult.Model; | |
//Assert | |
Assert.Null(ac.LocalFeatureDetail); | |
Assert.Null(ac.Parameter); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment