2011-06-27 85 views
2

我只是想测试一些涉及数据库的验证。我的测试代码的设置看起来是这样的:夏普架构2.0单元测试

[TestFixture] 
public class UserValidatorTester : RepositoryTestsBase 
{ 
    [SetUp] 
    public void Setup() 
    { 
     ServiceLocatorInitializer.Init(); 
     base.SetUp(); 
    } 


... 

的ServiceLocatorInitializer看起来是这样的:

public class ServiceLocatorInitializer 
     { 
      public static void Init() 
      { 
       IWindsorContainer container = new WindsorContainer(); 

       container.Register(
         Component 
          .For(typeof(IEntityDuplicateChecker)) 
          .ImplementedBy(typeof(EntityDuplicateChecker)) 
          .Named("entityDuplicateChecker")); 

container.Register(Component.For(typeof(ISessionFactoryKeyProvider)).ImplementedBy(typeof(DefaultSessionFactoryKeyProvider)).Named("sessionFactoryKeyProvider")); 


       ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container)); 
      } 
     } 

我越来越:

at SharpArch.Domain.SafeServiceLocator`1.GetService() 

在SharpArch.NHibernate.SessionFactoryKeyHelper.GetKeyFrom (Object anObject) at SharpArch.NHibernate.NHibernateRepositoryWithTypedId 2.get_Session() at EID2.Tasks.Repositories.UserRepository.SaveOrUpdate(User entity) in C:\Users\csetzkorn\Documents\Visual Studio 2010\Projects\EID2\Solutions\EID2.Tasks\Repositories\UserRepository.cs:line 17 at EID2.Tasks.UserTasks.CreateUser(CreateUserViewModel CreateUserViewModel) in C:\Users\csetzkorn\Documents\Visual Studio 2010\Projects\EID2\Solutions\EID2.Tasks\UserTasks.cs:line 33 at EID2.Tests.Validation.UserValidatorTester.LoadTestData() in C:\Users\csetzkorn\Documents\Visual Studio 2010\Projects\EID2\Solutions\EID2.Tests\Validation\UserValidatorTester.cs:line 39 at SharpArch.Testing.NUnit.NHibernate.RepositoryTestsBase.SetUp() --NullReferenceException at Microsoft.Practices.ServiceLocation.ServiceLocator.get_Current() at SharpArch.Domain.SafeServiceLocator 1.GetService()

基督教

PS:

包括dll文件:

<ItemGroup> 
    <Reference Include="Castle.Core"> 
     <HintPath>..\..\Packages\Castle.Core.2.5.2\lib\SL4\Castle.Core.dll</HintPath> 
    </Reference> 
    <Reference Include="Castle.Windsor"> 
     <HintPath>..\..\Packages\Castle.Windsor.2.5.3\lib\NET40\Castle.Windsor.dll</HintPath> 
    </Reference> 
    <Reference Include="CommonServiceLocator.WindsorAdapter"> 
     <HintPath>..\..\Packages\CommonServiceLocator.WindsorAdapter.1.0\lib\NET35\CommonServiceLocator.WindsorAdapter.dll</HintPath> 
    </Reference> 
    <Reference Include="EID2.Tasks"> 
     <HintPath>..\xxx.Tasks\bin\Debug\xxx.Tasks.dll</HintPath> 
    </Reference> 
    <Reference Include="FluentValidation, Version=2.0.0.0, Culture=neutral, PublicKeyToken=a82054b837897c66, processorArchitecture=MSIL"> 
     <SpecificVersion>False</SpecificVersion> 
     <HintPath>..\..\Packages\FluentValidation\FluentValidation.dll</HintPath> 
    </Reference> 
    <Reference Include="Microsoft.Practices.ServiceLocation"> 
     <HintPath>..\..\Packages\CommonServiceLocator.1.0\lib\NET35\Microsoft.Practices.ServiceLocation.dll</HintPath> 
    </Reference> 
    <Reference Include="MvcContrib.TestHelper"> 
     <HintPath>..\..\Packages\MvcContrib.Mvc3-ci.3.0.68.0\lib\MvcContrib.TestHelper.dll</HintPath> 
    </Reference> 
    <Reference Include="NHibernate, Version=3.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL"> 
     <SpecificVersion>False</SpecificVersion> 
     <HintPath>..\..\ReferencedAssemblies\NHibernate\NHibernate.dll</HintPath> 
    </Reference> 
    <Reference Include="NHibernate.ByteCode.Castle"> 
     <HintPath>..\..\ReferencedAssemblies\NHibernate\NHibernate.ByteCode.Castle.dll</HintPath> 
    </Reference> 
    <Reference Include="NHibernate.Validator"> 
     <HintPath>..\..\ReferencedAssemblies\NHibernate\NHibernate.Validator.dll</HintPath> 
    </Reference> 
    <Reference Include="nunit.framework"> 
     <HintPath>..\..\Packages\NUnit.2.5.10.11092\lib\nunit.framework.dll</HintPath> 
    </Reference> 
    <Reference Include="Rhino.Mocks"> 
     <HintPath>..\..\ReferencedAssemblies\RhinoMocks\Rhino.Mocks.dll</HintPath> 
    </Reference> 
    <Reference Include="SharpArch.Domain"> 
     <HintPath>..\..\ReferencedAssemblies\SharpArchitecture\SharpArch.Domain.dll</HintPath> 
    </Reference> 
    <Reference Include="SharpArch.NHibernate"> 
     <HintPath>..\..\ReferencedAssemblies\SharpArchitecture\SharpArch.NHibernate.dll</HintPath> 
    </Reference> 
    <Reference Include="SharpArch.Testing"> 
     <HintPath>..\..\ReferencedAssemblies\SharpArchitecture\SharpArch.Testing.dll</HintPath> 
    </Reference> 
    <Reference Include="SharpArch.Testing.NUnit"> 
     <HintPath>..\..\ReferencedAssemblies\SharpArchitecture\SharpArch.Testing.NUnit.dll</HintPath> 
    </Reference> 
    <Reference Include="SharpArch.Tests"> 
     <HintPath>..\..\ReferencedAssemblies\SharpArchitecture\SharpArch.Tests.dll</HintPath> 
    </Reference> 
    <Reference Include="System" /> 
    <Reference Include="System.ComponentModel.DataAnnotations" /> 
    <Reference Include="System.Core" /> 
    <Reference Include="System.Data.SQLite"> 
     <HintPath>..\..\Packages\System.Data.SQLite.1.0.66.0\lib\System.Data.SQLite.DLL</HintPath> 
    </Reference> 
    <Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> 
     <SpecificVersion>False</SpecificVersion> 
     <HintPath>..\..\ReferencedAssemblies\ASP.NET MVC\System.Web.Mvc.dll</HintPath> 
    </Reference> 
    <Reference Include="System.Xml.Linq" /> 
    <Reference Include="System.Data.DataSetExtensions" /> 
    <Reference Include="Microsoft.CSharp" /> 
    <Reference Include="System.Data" /> 
    <Reference Include="System.Xml" /> 
    </ItemGroup> 

PPS:

SQL精简版配置

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
    <session-factory> 
     <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> 
     <property name="dialect">NHibernate.Dialect.SQLiteDialect</property> 
     <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property> 
     <property name="connection.connection_string">Data Source=:memory:;Version=3;New=True;</property> 
     <property name="connection.release_mode">on_close</property> 
     <property name="show_sql">true</property> 
     <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property> 
    </session-factory> 
</hibernate-configuration> 
+0

确保您已将nhibernate配置包含到您的单元测试项目中。 – Peter

+0

这是def。包括为SQL精简版 – cs0815

回答

3

有几个可能出错的SQLite并有针对不同问题的不同解决方案的事,我想你遇到的问题是由于源码.dll文件针对.NET System.Data建,而你有什么是.NET 4 System.Data,试着加入:

<startup useLegacyV2RuntimeActivationPolicy="true"> 
<supportedRuntime version="v4.0"/> 
</startup> 

在测试的app.config配置元素。另外,如果您在x64机器上,并且正在引用x86 sqlite,那么您需要将您的测试程序集的目标框架更改为x86(右键单击项目,选择属性,生成选项卡,然后选择x86目标框架)

+0

谢谢。到达那里,但现在得到这个:ServiceLocator尚未初始化;我试图找回SharpArch.NHibernate.ISessionFactoryKeyProvider ----> System.NullReferenceException:对象未设置为一个对象的实例引用。 \t at SharpArch.Domain.SafeServiceLocator'1.GetService() \t at SharpArch.NHibernate.SessionFactoryKeyHelper。GetKeyFrom(object对象) \t在SharpArch.NHibernate.NHibernateRepositoryWithTypedId'2.get_Session()等 – cs0815

+0

您需要确保该DefaultSessionKeyProvider已注册温莎,确保你在你的ComponentRegistrar有这样的: '容器.Register( Component.For(typeof运算(ISessionFactoryKeyProvider)) .ImplementedBy(typeof运算(DefaultSessionFactoryKeyProvider)) .Named( “sessionFactoryKeyProvider”));' – ChrisR

+0

,也可以查看你以前的一个问题的答案: [链接] (http://stackoverflow.com/questions/5119444/sharp-architecture-problem/5119696#5119696) – ChrisR

0

的问题是,NHibernate会话工厂canno不会被创建。阅读错误信息:

System.ArgumentException : Unable to find the requested .Net Framework Data Provider. It may not be installed. 

您是否有SQLLite dll包含在您的项目中?

System.Data.SQLite.dll

+0

谢谢。包括dll。 – cs0815

+0

难道是NHibernate.Driver.SQLite20Driver是为.net 2.0,我正在与4.0或类似的东西? – cs0815

+0

奇怪 - 你能列出你的单元测试项目的Bin文件夹中的所有文件(当然除了你自己的自定义dll)。 – BonyT