单元测试Sitecore许可证管理器



我正在尝试使用Sitecore api进行单元测试。我在Visual Studio中的解决方案中得到了一个单元测试项目(MSTest)。我将configSections、connectionStrings、appSettings、sitecore、log4net元素从web.config复制到了app.config。

我尝试运行的测试是:

Sitecore.Context.Database = Factory.GetDatabase("master");
Item a = Sitecore.Context.Database.GetItem("/sitecore/content/Home");
Assert.IsFalse(a.HasChildren);

我在app.config文件中定义了我的数据文件夹,到核心数据库、master数据库和web数据库的连接字符串也在app.config.中设置

抛出的异常是:

System.TypeInitializationException: The type initializer for 'Sitecore.SecurityModel.License.LicenseManager' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'file:///c:inetpubbinSitecore.Nexus.dll' or one of its dependencies. The system cannot find the file specified.=== Pre-bind state information ===
LOG: User = NT AUTHORITYSYSTEM
LOG: Where-ref bind. Location = c:inetpubbinSitecore.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:BuildAgenttempbuildTmpSYSTEM_E1259 2012-07-16 14_42_19Outfoo.bar.Framework.Tests.Core.DLL.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:WindowsMicrosoft.NETFrameworkv4.0.30319configmachine.config.
LOG: Attempting download of new URL file:///c:/inetpub/bin/Sitecore.Nexus.dll.

奇怪的是,当我用"在当前上下文中运行测试"运行它时,我没有得到异常。当我在TeamCity中运行它或使用"在解决方案中运行所有测试"时,我会得到异常。显然,该文件不在c:\inetpub/bin/Sitecore.Nexos.dll 中

有人能告诉我我做错了什么吗?

编辑

当我添加HttpSimulator时,错误消失了。发生另一个错误:

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.

正在读取的设置是"HeartbeatInterval",此设置在我的app.config中。当我将Settings.GetSetting("HeartbeatInterval)"添加到我的快速监视中时,它会返回正确的值。这就像sitecore.kernel无法访问app.config?!

正如您在"EDIT"下面看到的那样:我没有使用HttpContext来设置webroot的本地路径。在我这样做之后,我解决了最初的问题

最新更新