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
1 vote
2 answers
62 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
58 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
127 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
148 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,092
1 vote
1 answer
123 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
2 votes
0 answers
214 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
0 votes
1 answer
157 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
  • 336
0 votes
1 answer
152 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 ...
NewbieHere's user avatar
0 votes
1 answer
60 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 ...
Baxter Lane's user avatar
0 votes
0 answers
161 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 : ...
David Buckell's user avatar
2 votes
1 answer
260 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,258
3 votes
1 answer
452 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,258
0 votes
0 answers
55 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? ...
NewbieHere's user avatar
0 votes
1 answer
147 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,720
2 votes
1 answer
84 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
3 votes
1 answer
205 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
0 votes
1 answer
402 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 ...
Suman Baul's user avatar
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
3 votes
1 answer
465 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
0 votes
1 answer
159 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
577 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 ...
Viktor's user avatar
  • 1
3 votes
1 answer
164 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 vote
1 answer
422 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
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,197
3 votes
1 answer
368 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
1 vote
1 answer
293 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
354 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
368 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
3 votes
1 answer
71 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
2 votes
1 answer
305 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
-1 votes
1 answer
530 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....
Rakesh's user avatar
  • 11
2 votes
1 answer
622 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
0 votes
1 answer
137 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
2 votes
1 answer
788 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
0 votes
1 answer
523 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
1 vote
1 answer
838 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
0 votes
1 answer
1k 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 ...
Percy's user avatar
  • 285
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
2 votes
2 answers
387 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
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 ...
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
  • 453
0 votes
2 answers
360 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
2 votes
2 answers
658 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
1 vote
1 answer
648 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
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
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
1 answer
568 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
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
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
314 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