Unit Testing Using Sitecore.FakeDb Throws Sitecore.Nexus instantiation error on GetItem

2

I'm a sitecore developer and I want to use Sitecore.FakeDb in my unit testing. I have the following code:

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void Test_ArticleController_With_SitecoreItem()
    {
        Sitecore.Data.ID about_us_itemId = Sitecore.Data.ID.NewID;
        Sitecore.Data.ID careers_itemId = Sitecore.Data.ID.NewID;

        using (var db = new Sitecore.FakeDb.Db
        {
            new Sitecore.FakeDb.DbItem("about-us", about_us_itemId)
            {
                new Sitecore.FakeDb.DbField(SitecoreFieldIds.WTW_REDIRECT_TO) { Value = "/WTW-Home/about-us/overview" }
            },
            new Sitecore.FakeDb.DbItem("careers", careers_itemId)
            {
                new Sitecore.FakeDb.DbField(SitecoreFieldIds.WTW_REDIRECT_TO) { Value = "http://careers.willistowerswatson.com" }
            }
        })
        {
            Sitecore.Data.Items.Item sampleItem3 = db.GetItem(about_us_itemId); // throws error
            Sitecore.Data.Items.Item sampleItem2 = db.GetItem("/sitecore/content/careers"); //throws error

            //Assert
            Assert.AreEqual("abc", "abc");
        };
    }
}

I get the following error when I try either of the two above .GetItem(...) lines:

System.TypeInitializationException: 'The type initializer for 'Sitecore.SecurityModel.License.LicenseManager' threw an exception.' InvalidOperationException: Could not instantiate the type 'Sitecore.Nexus.Licensing.NexusLicenseApi, Sitecore.Nexus'

I have included the following relevant references (amongst others) in my unit testing project: Sitecore.FakeDb, Sitecore.Kernel, Sitecore.Mvc, Moq, Glass.Mapper, Glass.Mapper.Sc, Glass.Mapper.Sc.Mvc, Castle.Core. Do I need Sitecore.Nexus? If yes, where can I get it from?

I have an App_Config/Include/Sitecore.FakeDb.config file, as well as an App_Config/app.config file, which has this setting in it:

<sitecore>
<settings>
    <setting name="LicenseFile" value="..\..\license.xml" />
</settings>
</sitecore>

Any idea on why this error is occuring and how to exactly fix it?

Here is the full stacktrace, if that helps:

System.TypeInitializationException occurred HResult=0x80131534 Message=The type initializer for 'Sitecore.SecurityModel.License.LicenseManager' threw an exception. Source=Sitecore.Kernel StackTrace: at Sitecore.SecurityModel.License.LicenseManager.DemandRuntime(Boolean acceptExpress) at Sitecore.Data.Managers.DefaultItemManager.get_FallbackProvider() at Sitecore.Data.Managers.DefaultItemManager.<>c__DisplayClass2f.b__2e() at Sitecore.Data.Managers.DefaultItemManager.ExecuteAndReturnResult[TArgs,TResult](String pipelineName, String pipelineDomain, Func1 pipelineArgsCreator, Func1 fallbackResult) at Sitecore.Data.Managers.DefaultItemManager.GetItem(ID itemId, Language language, Version version, Database database, SecurityCheck securityCheck) at Sitecore.Data.Managers.DefaultItemManager.GetItem(ID itemId, Language language, Version version, Database database) at Sitecore.Data.Managers.ItemManager.GetItem(ID itemId, Language language, Version version, Database database) at Sitecore.Data.DefaultDatabase.GetItem(ID itemId) at Sitecore.FakeDb.Db.GetItem(ID id) at WTW.Feature.HomeBottomContent.Tests.UnitTest1.Test_ArticleController_With_SitecoreItem() in C:\dev\TowersWatson\DEV\Maintenance\Source\Feature\HomeBottomContent\Tests\UnitTest1.cs:line 76

Inner Exception 1: InvalidOperationException: Could not instantiate the type 'Sitecore.Nexus.Licensing.NexusLicenseApi, Sitecore.Nexus'

c#
moq
sitecore8
sitecore-mvc
glass-mapper
asked on Stack Overflow Sep 18, 2017 by user3034243

2 Answers

4

You need to have a reference to Sitecore.nexus.dll

In Visual Studio: Tools->Options

In Options window find 'Nuget Package Manager'->'Package Sources' enter image description here

Then click green plus and fill Name and Source text fields.

Name one - whatever you want.

Source text field fill with 'https://sitecore.myget.org/F/sc-packages/api/v3/index.json'

After that go to Tools->Nuget Package Manager->Manage Nuget Packages for Solution... In 'Package source' dropdown in right-top corner choose just add source. In my case it is 'Sitecore'. In search field type 'Sitecore.Nexus.Noreferences':

enter image description here

Choose a project you want to install package and click Install button.

answered on Stack Overflow Sep 19, 2017 by Aleksey Shevchenko
3

The Sitecore.Nexus assembly is in the Bin folder of your Sitecore website. Add a reference to this assembly from your test project.

answered on Stack Overflow Sep 19, 2017 by Michael Edwards

User contributions licensed under CC BY-SA 3.0