C# 访问单元测试项目中的Sitecore上下文

C# 访问单元测试项目中的Sitecore上下文,c#,unit-testing,sitecore,sitecore8,C#,Unit Testing,Sitecore,Sitecore8,我想访问MS测试项目中的sitecore上下文。 我使用的是Sitecore 8.1和MS测试项目(框架:4.5.2),使用的是VS2005 我遵循了这里的一些说明 因此,我的单元测试项目中有所有必需的DLL和sitecore配置文件 我的代码如下: [TestMethod] public void TestMethod1() { State.HttpRuntime.AppDomainAppPath = Directory.GetCurrentDirect

我想访问MS测试项目中的sitecore上下文。 我使用的是Sitecore 8.1和MS测试项目(框架:4.5.2),使用的是VS2005 我遵循了这里的一些说明

因此,我的单元测试项目中有所有必需的DLL和sitecore配置文件

我的代码如下:

    [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);
    }
现在我得到以下错误:

        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.
我已经检查了许可证文件路径及其正确性。 我还补充了这一点

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


有人能帮我吗?

为了使用Sitecore.Context进行单元测试,您需要将所有Sitecore配置从应用程序配置复制到单元测试项目中,并跨
web.Config
进行复制,然后将其重命名为
App.Config
。您可以在此处找到更详细的演练:


但是,我建议您使用来运行单元测试。这将允许您更好地隔离测试,而无需依赖正在运行的Sitecore实例/数据库,并允许执行更多受控测试,因为数据不会因更新而不断更改。

Hello Debajit Mukherjee

我也陷入了同样的场景,我的模块与Sitecore.Context信息紧密耦合。所以,我想在测试中使用Sitecore.Context

你可以在下面找到更多关于如何实现它的信息。

希望对你有帮助

如果需要进一步澄清,请告诉我


谢谢

这可能与您的情况无关,但我遇到了一个非常类似的问题,最终发现这是因为Sitecore.Nexus DLL被阻止

在我的例子中,我从测试项目中删除了对Sitecore.Nexus的引用,取消了DLL的阻塞,重新添加了引用,一切都很好


请参阅:有关取消阻止DLL的详细信息

我已按照如下说明进行了操作:1)将web.config重命名为app.config 2)包括app_config文件夹下的所有配置文件3)包括所有DLL,但我仍然收到相同的错误。Sitecore fake DB将无法工作,因为我们也希望测试实际内容。新版本的sitecore是否会出现这种情况?我正在使用Sitecore 8.1更新1。@DebajitMukherjee,当你说FakeDB不能工作时?什么意思?我们用得很好。你有TDS的内容吗。如果你这样做,你可以使用