sshushliapin / Sitecore.FakeDb

Unit testing framework for Sitecore.
MIT License
117 stars 36 forks source link

Issue with Autofixture.AutoDbCustomization and NUnit3 #217

Closed ajvdburg closed 4 years ago

ajvdburg commented 4 years ago

Hi,

I've followed the instructions as per the wiki page on how to create and use the AutoDbAttribute class, but I'm getting the following error:

System.InvalidOperationException : Sitecore.FakeDb.Db instance has not been initialized.
   at Sitecore.FakeDb.Data.Engines.DataCommands.DataEngineCommand.get_DataStorage()

With xUnit this isn't issue, but since I'm trying to convert my test projects to NUnit, I get this error. Any ideas on how to solve this?

Best regards,

Arnold Jan van der Burg

sshushliapin commented 4 years ago

hi @ajvdburg, which sitecore version is used? could you please share the failing test?

ajvdburg commented 4 years ago

Hi Serhii,

Thank you for your quick reply. I'm using Sitecore 9.1.1 with nUnit 3.14.0, Autofixture 4.14.0, AutoFixture.NUnit3 v4.14.0 and AutoFixture.AutoNSubstitute 4.14.0 and off course FakeDb v 2.0.1. This is the test that is failing:

[Test] [AutoDbData] public void TestDatabaseFixture([Content] DbItem item, Database database) { var newItem = database.GetItem(item.ID); Assert.NotNull(newItem); }

It fails on the GetItem method.

Best regards,

Arnold Jan van der Burg

sshushliapin commented 4 years ago

Looks like the issue somewhere in NUnit's internals. Smth with parallel test execution which is a pita for FakeDb :'( (it's disabled for xunit ootb). I tried to disable parallel test execution for NUnit as described here but with no luck.. Still see NUnit's ParallelWorkItemDispatcher in call stack...

It needs additional research. PR is always welcome of course ;)

ajvdburg commented 4 years ago

Hi Serhii,

Did some poking around on my own, and found the issue. Is has to with new constructor of AutoFixture.NUnit3.AutoDataAttribute class. They've deprecated the old constructor in favor of a Func<> which causes the customization to run on a separate thread. Issue is solved when using the old constructor. Check out https://github.com/AutoFixture/AutoFixture/blob/master/Src/AutoFixture.NUnit3/AutoDataAttribute.cs. Thanks for pointing me in the right direction!

Best regards,

Arnold Jan van der Burg

sshushliapin commented 4 years ago

Hi @ajvdburg,

Interesting. I haven't done anything special but I'm glad I helped you to troubleshoot the problem :) I'll close this issue then.