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 ...
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/...
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.
...
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 ...
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 ...
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 ...
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 ...
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...
...
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"] = "...
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 ...
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 ...
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 ...
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 ...
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")
});
...
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 ...
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>(
...
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....
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, ...
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 ...
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()
{
// ...
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 ...
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 ...
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 ...
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 ...
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 ...
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....
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 ...
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>
&...
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.
...
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<...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
unit-testing × 58fakedb × 20
nsubstitute × 5
sitecore-api × 3
glass-mapper × 3
sxa × 2
content-search × 2
xconnect × 2
personalization × 2
pipelines × 2
jss × 1
configuration × 1
media-library × 1
upgrade × 1
tds × 1
helix × 1
templates × 1
unicorn × 1
habitat × 1
contact-facets × 1
rules-engine × 1
link-manager × 1
buckets × 1
components × 1
training-certification × 1