Skip to main content
23 votes
Accepted

How do I create an NSubstitute Sitecore item?

This is possible with Sitecore 8.2, since the GetChildren and Add methods became virtual with this version. (You could create an NSubstitute Sitecore 8.1 item, but NSubstiute would not be able to ...
Dan Solovay's user avatar
  • 2,171
13 votes
Accepted

How to get started with Sitecore Unit Testing with SitecoreFakeDb

Answers 1) Yes, that will get out of control. You are actually implementing your own implementation for faking the Sitecore database. There already exists a solution for this: https://github.com/...
Koen Heye's user avatar
  • 1,626
10 votes

How do I create an NSubstitute Sitecore item?

Mocking Pipeline Code in Sitecore before 8.2 If you are working in Sitecore before 8.2 your best bet would be to look into using Sitecore FakeDB; it offers an in-memory representation of Sitecore. ...
Steven Newstead's user avatar
7 votes

Technique for changing Sitecore configuration for unit tests

I abstract out the configuration in to one (usually one but could more if it's a large project) Class with a corresponding Interface. Then rather than changing the config - I inject a mocked Interface ...
Owen Niblock's user avatar
  • 1,512
6 votes

How to get item url without using Linkmanager?

If you're on Sitecore 8.2+, you can use abstract BaseLinkManager class (that's actually valid for all the old static managers). In combination with Sitecore Dependency Injection that is easy to inject ...
Serhii Shushliapin's user avatar
5 votes

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

Sitecore FakeDb does not set the context item because depending on the scenario, you might or might not need it to be set. If you need the context item not to be null, you can set it in your test ...
Serhii Shushliapin's user avatar
5 votes

Mock an Item in Nunit having fast Query

When writing unit tests for Sitecore my general rule of thumb is to try not to test whether it's doing its job, that's not your responsibility. I think you might be better off abstracting this away ...
Gravypower's user avatar
5 votes

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

You are using SXA ModelRepository which is using dependency injection and service locator pattern to inject for example: wrapper for Rendering wrapper PageContext ContentRepository few more things... ...
Dawid Rutkowski's user avatar
4 votes
Accepted

Technique for changing Sitecore configuration for unit tests

Assuming that you are using FakeDB for your Unit Tests you can simply change settings on the fly. See example: using (Db fakeDb = new Db()) { fakeDb.Configuration.Settings["SettingName"] = "...
Alan Płócieniak's user avatar
4 votes
Accepted

How to get item url without using Linkmanager?

LinkManager is essentially just a static wrapper around LinkProvider, which uses virtual methods for everything. If you simply inject LinkManager.Provider into your controller's constructor, you can ...
Richard Szalay's user avatar
4 votes
Accepted

Alternatives to using custom pipelines for improved unit testability

Had a bit of a think about how to do this: Test the processor steps So the first thing to do is make sure all your code logic is off loaded to a service or manager class. The processor class really ...
Richard Seal's user avatar
  • 20.8k
3 votes
Accepted

Setting up FakeSiteContext for urls

The simplest solution is to change your code to make it more easily unit testable, and remove the dependence on the Sitecore.Context object, and instead pass in the required variables as a parameter ...
jammykam's user avatar
  • 13.9k
3 votes

Mock an Item in Nunit having fast Query

As I stated in comments, it's a good practice to provide a Minimal, Complete, and Verifiable example. For this particular case, it seems everything works fine so I need more details to reproduce your ...
Serhii Shushliapin's user avatar
3 votes
Accepted

Problem retrieving FakeDb DbLinkField attributes

Ok I finally found my mistake. Since I am using DbTemplates, the DbLinkField needs to be added to the template first. db.Add(new DbTemplate("t1", t1_TemplateId)){ new DbLinkfield("theField") }); ...
jgondev's user avatar
  • 335
3 votes

Using TDS project items in FakeDb.Serialization

My problem was actually due to ReSharper Unit Test for NUnit runner. It was running tests from temporary location, not from the project output directory. Fix was to tell NUnit runner to set ...
jjczopek's user avatar
  • 353
3 votes
Accepted

How to mock XConnectClient

I was able to run your code by not mocking Uri, but by Mock<XConnectClient> client = new Mock<XConnectClient>( new Mock<XConnectClientConfiguration>( ...
grg's user avatar
  • 1,831
3 votes

Unit testing a Sitecore component controller throws exception in mocked service

By mocking a proper return type(GetPreviousProductsResponse) value for the injected service(_productService) instead of a model property(PreviousProducts) solved this issue. _productService....
Venu Vustipalli's user avatar
3 votes
Accepted

How to mock an ImageField in Sitecore using nSubstitute and xUnit

If you go into ImageField.MediaItem getter, you'll notice that the MediaDatabase.GetItem() method uses 3 parameters including language and version: this.mediaItem = this.MediaDatabase.GetItem(mediaId, ...
Serhii Shushliapin's user avatar
3 votes
Accepted

How do you unit test PageContext.Current.Item?

An attempt to utilize static prop PageContext.Current indeed can be cumbersome. While it is an Ambient Context, Sitecore implementation does not let you mock and set it easily. Even if you do, you'll ...
Serhii Shushliapin's user avatar
3 votes
Accepted

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

I can think of the below skeleton test. [TestClass] public class MyServiceTests { [TestMethod] public void GetItemByShortPath_Should_Return_Item() { // ...
Nikhil Kulkarni's user avatar
2 votes

Technique for changing Sitecore configuration for unit tests

In more recent releases of Sitecore (8.2 and later), settings are provided by an abstract class, Sitecore.Abstractions.BaseSettings, which is wired up to a DefaultSettings singleton via Sitecore's ...
Dan Solovay's user avatar
  • 2,171
2 votes

Technique for changing Sitecore configuration for unit tests

Recently, I have started using the SettingsSwitcher for mocking Sitecore <setting> values. I configure all of the mocked values in the constructor of a TestRunner class that I initialize before ...
Zachary Kniebel's user avatar
2 votes

URL encoding in HttpRequestArgs while unit testing

I'd suggest not to use FakeDb for (unit) testing such scenario. If I unrerstand your intention correctly, you do not test any logic of your application. You're trying to test a piece of Sitecore ...
Serhii Shushliapin's user avatar
2 votes
Accepted

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

You need to add template to the FakeDB database first. Just add those 2 lines to your code: var childTemplate = new DbTemplate("child", templateID); db.Add(childTemplate); e.g: var myHomeID = new ...
Marek Musielak's user avatar
  • 24.2k
2 votes

Mocking the Authentication Provider

For testing authentication using FakeDb you can try next : [Theory] [AutoDbData] public void Login_UserIsNotLoggedIn_ShouldReturnFalse(FakeMembershipUser user, AuthenticationProvider ...
Vlad Iobagiu's user avatar
  • 18.8k
2 votes
Accepted

Fake a Media Item for Unit Testing

There is a package from Sitecore.FakeDb that integrates with AutoFixture to generate items on the fly. The Nuget package is Sitecore.FakeDb.AutoFixture https://github.com/sergeyshushlyapin/Sitecore....
Vincent Lui's user avatar
2 votes

Unable to get AutoFixture, FakeDb and NUnit3 combination to work

I will start my answer with a question, why do you need to use FakeDB? Even though the idea of FakeDB is great in theory, it inevitably causes more issues than it solves, in my opinion, when testing ...
Gravypower's user avatar
2 votes
Accepted

Unable to switch page DisplayMode in unit test

It's a bug and needs to be fixed in FakeDb. But the good news is that you may hotfix it following the article instructions carefully: add shell site registration to your config: <sites> &...
Serhii Shushliapin's user avatar
2 votes
Accepted

FakeDB unit tests erroring

First, I'd strongly recommend you avoid using FakeDb for Sitecore 9. It still works, but Sitecore 9 (even 8.2+) contains abstractions that makes FakeDb unnecessary. See this post for more information. ...
Serhii Shushliapin's user avatar
2 votes
Accepted

Can we use Moq for Sitecore Item Axes?

Of course, it is possible. Moq has a different API, so I'd suggest to start from this guide first. This code sample configures item descendants to return an empty array: var itemMock = new Mock<...
Serhii Shushliapin's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible