Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sitecore 8.2 - Could not create instance of type: Sitecore.Data.Database. No matching constructor was found. #139

Closed
derekzernach opened this issue Sep 7, 2016 · 4 comments

Comments

@derekzernach
Copy link

If I use Sitecore.Kernel 8.2.160729 I get the below error. No AutoFixture.Xunit tests will run on the new Sitecore.Kernel 8.2.160729 DLL

Sitecore.Exceptions.ConfigurationException
Could not create instance of type: Sitecore.Data.Database. No matching constructor was found.
at Sitecore.Configuration.DefaultFactory.CreateFromTypeName(XmlNode configNode, String[] parameters, Boolean assert)
at Sitecore.Configuration.DefaultFactory.CreateObject(XmlNode configNode, String[] parameters, Boolean assert, IFactoryHelper helper)
at Sitecore.Configuration.DefaultFactory.CreateObject(XmlNode configNode, String[] parameters, Boolean assert)
at Sitecore.Configuration.DefaultFactory.CreateObject(String configPath, String[] parameters, Boolean assert)
at Sitecore.Configuration.DefaultFactory.GetDatabase(String name, Boolean assert)
at Sitecore.Configuration.DefaultFactory.GetDatabase(String name)
at Sitecore.FakeDb.Db..ctor(String databaseName)

@sshushliapin
Copy link
Owner

sshushliapin commented Sep 7, 2016

@derekzernach thanks for reporting the issue.

Sitecore 8.2 introduced new DI model. This impacted 'old good' Database class which has been split into two. Now there is abstract Database class and concrete DefaultDatabase which must be registered in the config.

To make your tests pass please add the Sitecore.FakeDb.config file to the <test project>/App_Config/Include folder:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <databases>
      <database id="core">
        <patch:attribute name="type">Sitecore.Data.DefaultDatabase, Sitecore.Kernel</patch:attribute>
      </database>
      <database id="master">
        <patch:attribute name="type">Sitecore.Data.DefaultDatabase, Sitecore.Kernel</patch:attribute>
      </database>
      <database id="web">
        <patch:attribute name="type">Sitecore.Data.DefaultDatabase, Sitecore.Kernel</patch:attribute>
      </database>
    </databases>
    <!-- EVENTING -->
    <eventing defaultProvider="sitecore">
      <providers>
        <clear />
        <add name="sitecore" type="Sitecore.Eventing.EventProvider, Sitecore.Kernel" systemDatabaseName="core" />
      </providers>
    </eventing>
  </sitecore>
</configuration>

You might also need to reference Microsoft DependencyInjection nuget package in tests and upgrade your solution to .NET 4.5.2.

@derekzernach
Copy link
Author

This definitely works! Can this patch config be referenced from the bin folder in any way? I'm using nuget to install this file along with a DLL to all my testing projects and the new version of nuget doesn't add files to projects anymore.

@sshushliapin
Copy link
Owner

@derekzernach yes it can. Despite FakeDb allows using auto-include files, they look a bit awkward when used in unit tests. The solution I suggested above could be applied without the necessity to update FakeDb but I'd definitely like to find more elegant way to solve this.

The databaseType setting in the App.config file is the best option I can think of:

<!-- DATABASE TYPE
     For Sitecore versions prior to 8.2 should be 'Sitecore.Data.Database, Sitecore.Kernel'.
     For Sitecore 8.2 and later should be 'Sitecore.Data.DefaultDatabase, Sitecore.Kernel'. -->
<sc.variable name="databaseType" value="Sitecore.Data.Database, Sitecore.Kernel" />

@sshushliapin
Copy link
Owner

Implemented in v1.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants