I'm trying to unit test with the Sitecore api. I got a unit test project (MSTest) in my solution in Visual Studio. I copied the configSections, connectionStrings, appSettings, sitecore, log4net elements from my web.config to my app.config.
The test I'm trying to run is:
Sitecore.Context.Database = Factory.GetDatabase("master");
Item a = Sitecore.Context.Database.GetItem("/sitecore/content/Home");
Assert.IsFalse(a.HasChildren);
I defined my datafolder in the app.config file and the connectionstrings to my core, master and web databases are also set in the app.config.
The exception that is thrown is:
System.TypeInitializationException: The type initializer for 'Sitecore.SecurityModel.License.LicenseManager' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'file:///c:\inetpub\bin\Sitecore.Nexus.dll' or one of its dependencies. The system cannot find the file specified.=== Pre-bind state information ===
LOG: User = NT AUTHORITY\SYSTEM
LOG: Where-ref bind. Location = c:\inetpub\bin\Sitecore.Nexus.dll
LOG: Appbase = file:///C:/BuildAgent/temp/buildTmp/SYSTEM_E1259 2012-07-16 14_42_19/Out
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\BuildAgent\temp\buildTmp\SYSTEM_E1259 2012-07-16 14_42_19\Out\foo.bar.Framework.Tests.Core.DLL.config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Attempting download of new URL file:///c:/inetpub/bin/Sitecore.Nexus.dll.
The strange thing is, when i run it with "Run Test in Current Context" I don't get an exception. When I run it in TeamCity or with "Run All Tests in Solution" I get the exception. Obviously the file isn't located in c:/inetpub/bin/Sitecore.Nexus.dll
Can anyone tell me what I did wrong?
EDIT
When I added an HttpSimulator the error disappeared. Another error occurred:
Test method foo.Bar.Framework.Tests.Core.UnitTest1.TestMethod1 threw exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: The type initializer for 'Sitecore.Services.Heartbeat' threw an exception. ---> System.InvalidOperationException: Could not read Sitecore configuration.
The setting which is being read is "HeartbeatInterval" and this setting IS in my app.config. When I add Settings.GetSetting("HeartbeatInterval") to my quick watch it returns the right value. It is like the sitecore.kernel cannot access the app.config?!
As you can see below the "EDIT": i wasn't using a HttpContext in which I could set the local path of the webroot. After I did that, I resolved the initial issue