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):
[Fact]
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:
Message:
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)
Db.Dispose()
Class1.HowToCreateSimpleItem() line 22
What am I missing here?
I'm using Sitecore 9.3, in case that's relevant.
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.