Access Sitecore context in Unit test project

0

I would like to access sitecore context in MS Test Project. I am using Sitecore 8.1 and the MS test project (Framework: 4.5.2) using VS 2005 I followed some of the instructions found here http://getfishtank.ca/blog/unit-testing-in-sitecore-with-context-and-app-config

So I have all the required DLLS and sitecore config files in my unit test project.

My code is below:

    [TestMethod]
    public void TestMethod1()
    {
        State.HttpRuntime.AppDomainAppPath = Directory.GetCurrentDirectory();

        string dataFolder = Sitecore.Configuration.Settings.DataFolder;
        string licenseFile = Settings.LicenseFile;

        var db = Sitecore.Configuration.Factory.GetDatabase("master");
        var home = db.GetItem("/sitecore/content/");
        Assert.AreEqual(5, 5);
    }

Now I am getting error below:

        Result StackTrace:  
    at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
       at System.Reflection.Assembly.LoadFrom(String assemblyFile)
       at Sitecore.Reflection.ReflectionUtil.LoadAssembly(String name)
       at Sitecore.Reflection.ReflectionUtil.CreateObject(String assembly, String className, Object[] parameters)
       at Sitecore.Reflection.ReflectionUtil.CreateObject(String typeName, Object[] parameters)
       at Sitecore.Reflection.Nexus.GetApi[T](String typeName, T& api)
       at Sitecore.SecurityModel.License.LicenseManager.GetSnapshotData(Guid instance)
       at Sitecore.SecurityModel.License.LicenseManager.UpdateSnapshot()
       at Sitecore.SecurityModel.License.LicenseManager..cctor()
     --- End of inner exception stack trace ---
        at Sitecore.SecurityModel.License.LicenseManager.DemandRuntime(Boolean acceptExpress)
       at Sitecore.Data.Managers.ItemManager.get_Provider()
       at Sitecore.Data.Managers.ItemManager.GetItem(String itemPath, Language language, Version version, Database database)
       at Sitecore.Data.Database.GetItem(String path)
       at MSUnitTestSitecore.UnitTest1.TestMethod1() in C:\Devarea_Debajit\MyProjects\MSUnitTestSitecore\MSUnitTestSitecore\UnitTest1.cs:line 20
    Result Message: 
    Test method MSUnitTestSitecore.UnitTest1.TestMethod1 threw exception: 
    System.TypeInitializationException: The type initializer for 'Sitecore.SecurityModel.License.LicenseManager' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'file:///C:\Devarea_Debajit\MyProjects\MSUnitTestSitecore\MSUnitTestSitecore\bin\Debug\Sitecore.Nexus.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515) ---> System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.

I have checked the license file path and its correct. I have also added this

 <runtime>
    <loadFromRemoteSources enabled="true"/>
 </runtime>

Can anyone help me ?

c#
unit-testing
sitecore
sitecore8

3 Answers

7

In order to unit test using Sitecore.Context, you will need to copy all Sitecore configs from the App_Config into your unit testing project as well as copying across the web.config and renaming it to app.config. You can find a more detailed walkthrough here: http://getfishtank.ca/blog/unit-testing-in-sitecore-with-context-and-app-config

I would however suggest you use Sitecore FakeDb to run your unit tests. This will allow you to isolate your test better without reliance on a runnign Sitecore instance/database and allowing for more controlled tests to be executed since the data will not be constantly changing due to updates.

answered on Stack Overflow Mar 10, 2016 by jammykam
0

Hello Debajit Mukherjee,

I have fallen into the same scenario, where my modules were tightly coupled with Sitecore.Context information. So, i was wanted to use Sitecore.Context into my tests.

You can find more information below on how to achieve it. https://pratiksatikunvar.wordpress.com/2016/04/04/unit-testing-in-sitecore-using-nunit-with-sitecore-context-information/

Hope it helps you.

Please let me know if any further clarification required.

Thanks

answered on Stack Overflow Apr 4, 2016 by PratikSatikunvar
0

This may not be relevant in your case but I had a very similar issue and eventually found that it was because the Sitecore.Nexus DLL was blocked.

In my case I removed that reference to Sitecore.Nexus from my test project, unblocked the DLL, re-added the reference and all was well.

See: http://www.limilabs.com/blog/unblock-dll-file for details of unblocking DLLs

answered on Stack Overflow Apr 27, 2016 by Kevin Owen

User contributions licensed under CC BY-SA 3.0