本文介绍了单元测试 Sitecore LicenseManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我尝试运行的测试是:

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

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

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.

抛出的异常是:

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.

奇怪的是,当我使用在当前上下文中运行测试"运行它时,我没有得到异常.当我在 TeamCity 中或使用在解决方案中运行所有测试"运行它时,出现异常.显然该文件不在 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?

编辑

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

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.

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

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?!

推荐答案

正如您在编辑"下方看到的那样:我没有使用 HttpContext,我可以在其中设置 webroot 的本地路径.在我这样做之后,我解决了最初的问题

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

这篇关于单元测试 Sitecore LicenseManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 19:25