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 referred in the Installation wiki).

I believe I've set the project up correctly in terms of app.config and license.xml file, but after adding some additional missing dependencies I'm still getting an error running a simple unit test (which I copied/pasted from the FakeDB wiki):

public void HowToCreateSimpleItem()
  using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
      new Sitecore.FakeDb.DbItem("Home") { { "Title", "Welcome!" } }
    Sitecore.Data.Items.Item home = db.GetItem("/sitecore/content/home");
    Xunit.Assert.Equal("Welcome!", home["Title"]);

The error I'm getting:

    System.MethodAccessException : Attempt by method 'Sitecore.FakeDb.Pipelines.ReleaseFakeDb.ResetSettings.Process(Sitecore.Pipelines.PipelineArgs)' to access method 'Sitecore.Configuration.Settings.Reset()' failed.
  Stack Trace: 
    (Object , Object )
    PipelineMethod.Invoke(Object argument)
    CorePipeline.Run(PipelineArgs args)
    DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain, Boolean failIfNotExists)
    DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
    DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args)
    Db.Dispose(Boolean disposing)
    Class1.HowToCreateSimpleItem() line 22

What am I missing here?

I'm using Sitecore 9.3, in case that's relevant.

  • You're probably going to find this unhelpful, but if you need FakeDB you're already doing it wrong. You don't need to unit test Sitecore, so checking if DB.GetItem() returns an item is a waste of time and doesn't prove anything. You need to unit test your own code. You should have shims and proxy classes and interfaces to abstract away your dependencies, including Sitecore itself. If you find yourself reaching for FakeDB you've already breached the Dependency Inversion principal, and you need to look at removing the dependencies from inside your code. – Richard Hauer May 5 at 10:02
  • PS. I appreciate your code was just a sample - that's not what I meant. There's no point in unit testing any of Sitecore's APIs for the same reason, no matter how much more complicated the example is. Give me 15 mins and I will show you a better way! – Richard Hauer May 5 at 10:03
  • Thanks @RichardHauer. If you've got a better way of doing so (or links to resources) that would be awesome! – Trayek May 5 at 13:08
  • I feel like there are some major elements missing from the way devs learn to do unit tests, and moreover TDD. Find me on Sitecore Slack and pm me and if we have an overlapped timezone I'll be happy to share. I should probably write a blog post about it. – Richard Hauer May 5 at 14:15

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.

Regarding the error. Please ensure you referenced proper FakeDb version. For Sitecore 9.3 it should be FakeDb 3.0.0-alpha1 (see release notes).

| improve this answer | |
  • Thanks. I'll have a read through the post and update :-) – Trayek May 5 at 13:00
  • @Trayek Can you please update how you resolved this? I am facing the same issue. – NewbieHere Jun 2 at 7:10
  • > Please ensure you referenced proper FakeDb version. For Sitecore 9.3 it should be FakeDb 3.0.0-alpha1 (see release notes). – Serhii Shushliapin Jun 2 at 8:25
  • @NewbieHere As mentioned in the previous comment change the FakeDB version to the alpha version - I actually ended up rewriting a lot of my code to apply dependency injection, use interfaces and proxy classes as RichardHauer says in the comments to the question. – Trayek Jun 2 at 9:00
  • but sitecore.fakedb's 3.0.0 alpha version is not present in nuget. I can find only upto 2.0.1. – NewbieHere Jun 2 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.