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
25 votes
2 answers
3k 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 ...
Dan Solovay's user avatar
  • 2,001
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 ...
uioporqwerty's user avatar
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 ...
Samsung Note's 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 ...
Mark-Danney's user avatar
5 votes
1 answer
595 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 ...
Matthew Dresser's user avatar
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"&...
Zachary Kniebel's user avatar
3 votes
1 answer
564 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 ...
Dean OBrien's user avatar
  • 1,560
3 votes
1 answer
243 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 ...
Venu Vustipalli's user avatar
3 votes
1 answer
173 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> ...
TheHvidsten's user avatar
  • 1,004
3 votes
1 answer
73 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 ...
jjczopek's user avatar
  • 353
3 votes
1 answer
510 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("{...
Ethan Schofer's user avatar
3 votes
1 answer
2k 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 ...
Trayek's user avatar
  • 1,207
3 votes
2 answers
547 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 ...
laytonc's user avatar
  • 31
3 votes
1 answer
394 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 ...
Masoud's user avatar
  • 151
2 votes
3 answers
601 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("...
jgondev's user avatar
  • 335
2 votes
1 answer
659 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); ...
Mark Smith's user avatar
2 votes
1 answer
815 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>()...
anita's user avatar
  • 35
2 votes
2 answers
686 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). ...
jjczopek's user avatar
  • 353
2 votes
1 answer
327 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 ...
Dean OBrien's user avatar
  • 1,560
2 votes
1 answer
93 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 ...
TheHvidsten's user avatar
  • 1,004
2 votes
1 answer
339 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 ...
qwert's user avatar
  • 389
2 votes
2 answers
391 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....
SoccerZortz's user avatar
2 votes
0 answers
310 views

Mock useSitecoreContext in Nextjs unit tests

I'm trying to write simple unit tests with Jest and mock useSitecoreContext. When I'm mocking the useSitecoreContext like this: useSitecoreContext: jest.fn() I'm getting an error "Cannot ...
Max's user avatar
  • 21
1 vote
2 answers
88 views

How to write unit test case for the static class and methods

I have below static class and method written to get the Sitecore Item by its path. public static class MyService { public static Item GetItemByShortPath(SiteContext siteContext, string ...
Alok Kumar's user avatar
1 vote
2 answers
71 views

DateUtil.ToUniversalTime() and DateUtil.ToServerTime() return unexpected values in unit tests

I have a case where the following code returns a different result depending on if I am debugging the site, or running the code in a unit test: var start = DateUtil.ToUniversalTime(@event.Start_Date); ...
Marcel Gruber's user avatar
1 vote
1 answer
167 views

How do you unit test PageContext.Current.Item?

I'm currently creating a unit test for an action result method:- public virtual ActionResult MyControllerTest(){ var pageItem = PageContext.Current.Item ... return ViewOrErrorResult<string>(...
Borj's user avatar
  • 145
1 vote
1 answer
898 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(...
anita's user avatar
  • 35
1 vote
1 answer
1k 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 ...
Suman Baul's user avatar
1 vote
1 answer
497 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 ...
BalaSM's user avatar
  • 51
1 vote
1 answer
384 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 { ...
Akshay Mahajan 's user avatar
1 vote
1 answer
2k 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 ...
qwert's user avatar
  • 389
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 ...
Jojo's user avatar
  • 463
1 vote
1 answer
207 views

Sitecore Unit Testing Trouble w/ NSubstitute and Nunit

I am newer to Sitecore and extremely new to its unit testing. been following examples online that mimic exactly what I am trying to accomplish but I seem to be getting an object reference(you will see ...
pet3r15's user avatar
  • 13
1 vote
1 answer
205 views

Error converting HttpRequest to HttpRequestBase for unit test

We are doing an upgrade from 8.2 to 10.2, and the code has a lot of unit tests. One element in a number of them involves the HttpRequestBegin pipeline, where there's an article about the code to use ...
Ken McAndrew's user avatar
  • 5,102
1 vote
1 answer
312 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 [...
LinkedListT's user avatar
1 vote
1 answer
387 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 ...
Chandana's user avatar
1 vote
1 answer
662 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 ...
Coen van Campenhout's user avatar
1 vote
0 answers
26 views

Sitecore Mock Unit testing throwing conversion error [closed]

Im trying to Mock sitecore item. It throws error cannot convert Moq.Mock to sitecore.Data.Items.Item, any work around for the Moq to work var item= GetMockItem(); _testing.GetChildren(item); --> ...
sns's user avatar
  • 403
1 vote
0 answers
68 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....
c-bro's user avatar
  • 557
1 vote
0 answers
329 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 ...
doctorwho's user avatar
1 vote
0 answers
778 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....
Gatogordo's user avatar
  • 20k
0 votes
2 answers
369 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 ...
rams's user avatar
  • 317
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 ...
rams's user avatar
  • 317
0 votes
2 answers
3k 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 ...
Sreekrishnan's user avatar
0 votes
1 answer
61 views

Unit testing logic with Template.InheritsFrom [closed]

Problem Is there a way to mock Template.InheritsFrom using NSubstitute? This is needed in order to be able to test logic that depends on the presence of a base template in the inheritance chain. In ...
Dan Solovay's user avatar
  • 2,001
0 votes
1 answer
189 views

Implement unit test for a sitecore MVC solution with glassmapper

I have the below scenario to implement unit testing on a Sitecore MVC (with glassmapper) integration. We are using a wrapper class to make calls to Glassmapper Sitecore Context methods. Below is one ...
D J's user avatar
  • 385
0 votes
1 answer
163 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 (...
Swati Gupta's user avatar
  • 2,982
0 votes
1 answer
174 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(...
gadgetgem's user avatar
0 votes
1 answer
140 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 ...
Steve Ward's user avatar
0 votes
1 answer
547 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. ...
username-4711's user avatar