Questions tagged [unit-testing]

Only for questions that are specific to unit testing Sitecore-based features, controls, renderings and other Sitecore-specific application logic

Filter by
Sorted by
Tagged with
0 votes
1 answer
45 views

Unit Test in Sitecore 9.3 SXA

I am working on upgrading a project from Sitecore 8.2 to 9.3. Few tests are failing and throwing NullReferenceException at Sitecore.XA.Foundation.Mvc.Repositories.Base.FillBaseProperties(model). While ...
user avatar
0 votes
1 answer
26 views

Seeking Wisdom on Automated Integration Testing Through all Environments, from Local Dev to Production

I want to run tests in all of My Environments, that are not unit tests but Integration tests to confirm Well in this case that Security is setup correctly on all items and that the users we in Roles ...
user avatar
0 votes
0 answers
69 views

How to unit test a custom field validator

I have implemented my own field validator that ensures a thumbnail image is always set on a rendering item. Working example: [Serializable] public class MandatoryThumbnailFieldValidator : ...
user avatar
2 votes
1 answer
58 views

How to mock MediaManager in Sitecore using nSubstitute and xUnit

I am in the process of setting up some unit tests for a Sitecore project (using this guide as a starting point: https://www.codeflood.net/blog/2020/05/17/logicless-view-itemless-model/) I have come ...
user avatar
3 votes
1 answer
92 views

How to mock an ImageField in Sitecore using nSubstitute and xUnit

I am in the process of setting up some unit tests for a Sitecore project (using this guide as a starting point: https://www.codeflood.net/blog/2020/05/17/logicless-view-itemless-model/) I have come ...
user avatar
0 votes
0 answers
33 views

Unit Test Fails at AuthenticationManager.Login(virtualUser)

We have a unit test for our Login method using Sitecore 8.2. It fails at the below piece of code and attached the error screenshot follows. Can anybody please let me know how to resolve this error? ...
user avatar
0 votes
0 answers
32 views

running unit tests in azure build pipeline for a helix project

We are trying to update our Azure build pipeline to run our Unit tests for our SC 8.2 instance. Our project is structured using helix and our build / publish is done using Gulp. It looks to me like ...
user avatar
0 votes
1 answer
79 views

Unit Test for Sitecore Personalization Rule

I am working on a Unit test(MSTest) with Sitecore 9.3. I have created one personalization rule, below is the sample code: protected override bool Execute(T ruleContext) { if (...
user avatar
  • 1,728
2 votes
1 answer
50 views

How to mock XConnect contact

I'm writing unit tests and using Moq as a mocking tool. In a previous question I was advised to use IXdbContext to mock the XConnect client, which works just fine. However, I run into problems if I ...
user avatar
3 votes
1 answer
106 views

Unit testing a Sitecore component controller throws exception in mocked service

I'm using xUnit, NSubstitute, FluentAssertions and Sitecore FakeDb. Here is the controller that needs a test, public class TransactionsController : Controller { private readonly ...
user avatar
0 votes
1 answer
247 views

Unit Test fails to run on solution based on Helix Visual Studio Templates

my project solution is based on Sitecore Helix Visual Studio Templates by Ander's Laub. Im trying to write Unit tests using XUnit, but none of the tests are running and not even failing. For test ...
user avatar
1 vote
1 answer
323 views

Create Unit Tests for Testing a Component in Sitecore 10

I'm completely new in Unit testing, trying to implement Unit testing for a navigation header component using xUnit, Moq and FluentAssertions library. Below are my code for Navigation Header ...
user avatar
3 votes
1 answer
204 views

Unit Testing with GlassMapper and GetItemByIdOptions

I have some code that retrieves a glass item like this: var root = this.RequestContext.SitecoreService.GetItem<ILookupFolderTemplate>( new GetItemByIdOptions(new Guid("{...
user avatar
0 votes
1 answer
84 views

Mock SiteSettingsProvider with Glass Mapper

Does anyone know how I can go about mocking the SiteSettingsProvider in Habitat? Item sitecoreItem= mvcContext.GetContextItem<Item>(); Item settingsItem= settingsProvider.GetSetting(...
user avatar
0 votes
1 answer
238 views

Item from Database.GetItem gets NullReference exception in field HasClones and in FieldRenderer.Render in test

I am trying to set up a simple test project to initially assert that my local C# models correspond to my Sitecore templates. The Item retrieval is successful using Database.GetItem(itemPath) and I can ...
user avatar
  • 1
3 votes
1 answer
102 views

How to mock XConnectClient

I'm writing unit tests for a class that uses XConnectClient, which means I need to mock an XConnectClient. I've tried the following using Moq and RandomTestValues packages: Mock<XConnectClient> ...
user avatar
1 vote
1 answer
191 views

Can we use Moq for Sitecore Item Axes?

I have converted to my code from Nsubstitute to Moq. Nsubstitute: var item = Substitute.For<Item>(id, itemData, database); item.Axes.Returns(Substitute.For<ItemAxes>(item)); return ...
user avatar
  • 51
2 votes
1 answer
1k views

FakeDB unit tests erroring

I'm trying to get some unit tests running in my Sitecore environment, where I'm using FakeDB as the testing framework. I've added the relevant NuGet packages in my Visual Studio project (which are ...
user avatar
  • 1,073
2 votes
1 answer
240 views

FakeDB runs into error when unit tests executed in parallel

I am facing a weird problem with Sitecore 9.2.0 and FakeDb 2.0.1. Some of the unit tests fails randomly with the following error. System.NullReferenceException Object reference not set to an instance ...
user avatar
  • 141
1 vote
1 answer
226 views

Sitecore 9.2 Unit Test Error: Could not find configuration node: contentSearch/configuration

I am trying to configure a Unit Test project within my Feature directory i.e. Feature Activity ExampleProject ExampleProject.UnitTest Implementation In Unit Test Project: UnitTest.cs [...
user avatar
1 vote
1 answer
239 views

Service Sitecore.Abstractions.ICorePipeline was not registered in container

I am writing unit test to read from ContentSearchManager.GetIndex method. At line ContentSearchManager.GetIndex(indexableItem) in below code : Sitecore.Context.SetActiveSite("<<Site ...
user avatar
1 vote
1 answer
283 views

Unable to switch page DisplayMode in unit test

I am trying to test one of the properties which checks for the PageMode.IsExperienceEditor. Reduced implementation of my property is given below: public ContentResult DataSourceNotConfiguredResult { ...
user avatar
3 votes
1 answer
70 views

Using TDS project items in FakeDb.Serialization

I have a test project for a module where I'd like to use FakeDb. Because I have a TDS project where store items for master database, I'd like to use them for tests with FakeDb.Serialization. When I ...
user avatar
  • 343
2 votes
1 answer
194 views

Convert item to bucket in C# code

We have a code in our solution that checks if the item is a bucket using Sitecore.Buckets.Managers.BucketManager.IsBucket(bucketItem) I'm creating a unit test using fakeDb and my code is this var ...
user avatar
  • 379
-1 votes
1 answer
439 views

Sitecore FakeDB Set Template Standard values

I am working on unit tests. So I am using fake DB. I am trying to set template standard values. So I used below code. Item newItem = db.GetItem(contextId); var standardValues = newItem.Template....
user avatar
  • 11
2 votes
1 answer
516 views

Unable to get AutoFixture, FakeDb and NUnit3 combination to work

Has anyone been able to get the combination of AutoFixure, FakeDb and NUnit3 to work? I am unable to retrieve an item I have added to the database (using example from FakeDb AutoFixture wiki page); ...
user avatar
0 votes
1 answer
123 views

Sitecore FakeDB Deserialisation and Unicorn

Using Sitecore 9.0.1, Im looking at using Sitecore Fake DB for unit testing. Im interested in using the Deserialisation extension so that I can test using content that's been serialised to disk ...
user avatar
2 votes
1 answer
649 views

Fake a Media Item for Unit Testing

Is possible to autogenerate a Media "element" ? I am trying to do this mediaProvider.GetMedia(Arg.Is<Sitecore.Data.Items.MediaItem>(i => i.ID == mediaItemId)).Returns(Arg.Any<Media>()...
user avatar
  • 35
0 votes
1 answer
431 views

Faking a MediaRequest and Media Items

I am currently trying to mock a media request and the media item I am requesting using FakeDb but for some reason my request is returning an empty MediaPath rather than the MediaPath I am expecting. ...
user avatar
1 vote
1 answer
566 views

Setting up FakeSiteContext for urls

I am currently trying to create a unittest for a override I made, but unittesting it seems a bit more difficult than I expected.. public class Extension : Original { public fallback(...
user avatar
  • 35
0 votes
1 answer
845 views

Unit testing Sitecore ContentSearchManager

I have the following code that returns Items: public static IList<Item> GetAll(Language language) { var index = $"my_index_{Context.Database.Name.ToLowerInvariant()}"; using (var ...
user avatar
  • 285
0 votes
2 answers
2k views

Create mock tests for the Sitecore logic

Have anyone used Mock Test framework for unit testing? I need to perform an unit testing for parent child functionality and am not able to get it resolved, because i am not getting a parent child ...
user avatar
2 votes
2 answers
380 views

Sitecore 9 eLearning TAC.Sitecore.Abstraction errors with code in Student resource file SitecoreItem.cs

In Module 5, I am getting an error on the Creating a Breadcrumb Component. I brought in the TAC.Sitecore.Abstractions project like the directions say and I added the reference for it in my event.tac....
user avatar
1 vote
1 answer
1k views

Fake DB on Sitecore.data.Items.Item.add method

I'm creating a unit test for a code that has an Item.Add method used and trying to use Sitecore Fake DB for this and I'm getting an error that it's not being populated. A sample code below: public ...
user avatar
  • 379
1 vote
1 answer
1k views

Mocking the Authentication Provider

I'm trying to Unit test my login method, for that I used below provider var provider = Substitute.For<Sitecore.Security.Authentication.AuthenticationProvider>(); provider ...
user avatar
  • 443
0 votes
2 answers
317 views

Mock an Item in Nunit having fast Query

I have a fast query : /sitecore/content/Countries/* I tried to mock them using nunit as using (Db db = new Db() { new DbItem("Countries"){ new ...
user avatar
  • 317
2 votes
2 answers
579 views

URL encoding in HttpRequestArgs while unit testing

I'm putting together unit tests for item resolver running under httpRequest pipeline. One of the test is checking if the item is properly resolved using display name with asian characters (Japanese). ...
user avatar
  • 343
1 vote
1 answer
599 views

Unit testing Sitecore custom rules

For a project I am using a bunch of custom Personalization rules, the only problem I am facing at the moment is that it takes quite a while to load the rules and I can't find any good examples of a ...
user avatar
0 votes
1 answer
2k views

Sitecore.Context.Item is Overwritten by RenderingContext.Current.Rendering

I'm new to Sitecore FakeDb NUnit Testing. While testing the code, I found that Sitecore.Context.Item = null; RenderingContext.Current.Rendering.DataSource = fakeItem.ID.ToString(); Since the item ...
user avatar
  • 317
5 votes
3 answers
2k views

How to get item url without using Linkmanager?

Hello i need to get the url from a Sitecore ítem but without using the LinkManager class from Sitecore because i can't mock it. What is the best approach to do unit tests when i need information from ...
user avatar
5 votes
1 answer
518 views

Alternatives to using custom pipelines for improved unit testability

What are common or effective approaches for making Pipelines in Sitecore unit testable? I recently updated some code to use custom pipelines and then found I could no longer unit test it :-( In the ...
user avatar
5 votes
2 answers
1k views

Unit testing SXA component with FakeDB - No locator provider is configured

I'm using FakeDB to try and set up unit tests for a custom component. While trying to run it, I walked into a couple of missing .dll's which I hadn't expected since the FakeDB install page hadn't ...
user avatar
1 vote
0 answers
63 views

Using JustMock With Sitecore 8.2.4 Kernel DLL

I have the following JustMock call in my old 8.0.7 Sitecore project: var siteGuid = Guid.NewGuid(); var siteItem = Mock.Create<Item>(); var id = new ID(siteGuid); Mock.Arrange(() => siteItem....
user avatar
  • 547
1 vote
0 answers
274 views

Analytics Tracking Unit Test

I need to create a unit test that will use the following pipeline Sitecore.Analytics.Pipelines.StartTracking It then needs to see if the tracking is enabled and then check if a cookie exists. I saw ...
user avatar
3 votes
2 answers
519 views

Mock MediaProvider on 8.2

I am using FakeDB trying to mock a media provider. I get this message when attempting the example shown in the wiki. Starting from Sitecore 8.2, the Sitecore.Resources.Media.MediaProvider class is ...
user avatar
  • 31
1 vote
0 answers
654 views

Unit testing contentsearch with facets

I am trying to write a test for this code: var publicationYears = new List<string>(); using (var context = searchContextBuilder.GetSearchContext(Indexes.NewsIndex)) { var query = context....
user avatar
  • 19.1k
2 votes
3 answers
526 views

Problem retrieving FakeDb DbLinkField attributes

db.Add(new DbTemplate("t1", t1_TemplateId)); db.Add(new DbTemplate("T", T_TemplateId) { BaseIDs = new[] { t1_TemplateId} }); db.Add(new DbItem("root") { new DbItem("...
user avatar
  • 325
3 votes
4 answers
2k views

Technique for changing Sitecore configuration for unit tests

When we unit test we often need to change some Sitecore configuration for the tests. For example, we may have the following: <sitecore> <configuration> <someNode name="myService"&...
user avatar
8 votes
1 answer
8k views

How to get started with Sitecore Unit Testing with SitecoreFakeDb

I'm trying to find the correct approach to begin unit testing the piece of code below: public class NameUtility { private readonly string _folder = Settings.GetSetting("FolderId"); public ...
user avatar
25 votes
2 answers
2k views

How do I create an NSubstitute Sitecore item?

Is it possible to create a mock Sitecore (using Sitecore 8.2) item using NSubstitute? I would like to write a unit test to cover the following pipeline processor code, which adds a child folder item ...
user avatar
  • 1,890