测试Spring.NET注入的成员资格提供程序

时间:2012-01-24 04:24:35

标签: unit-testing dependency-injection asp.net-membership integration-testing spring.net

喜欢注入会员提供商的灵活性,但我正试图让集成测试通过。我的Web应用程序版本在我的服务层中注入会员提供程序没有问题,但我无法在我的单元/集成测试应用程序版本中配置/正常工作。以下是我为会员提供商配置的所有内容。

App.config中:

<configuration>

  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
    </sectionGroup>
  </configSections>

  <spring>
    <context>
      <resource uri="assembly://App.UI/App.UI/auth-config.xml"/>
      <resource uri="assembly://App.UI/App.UI/core-config.xml"/>
    </context>
  </spring>

  <connectionStrings>
    <add name="ApplicationServices" providerName="System.Data.SqlClient"
         connectionString="server=localhost;database=aspnetdb;User ID=AppWebUser;Password=p@ssw0rd" />
    <add name="appDb" providerName="System.Data.SqlClient"
         connectionString="server=localhost;database=app;Persist Security Info=False;User ID=AppWebUser;Password=p@ssw0rd"/>
    <add name="test_appDb" providerName="System.Data.SqlClient"
              connectionString="server=localhost;database=app_test;Persist Security Info=False;User ID=AppWebUser;Password=p@ssw0rd"/>
  </connectionStrings>

  <system.web>
    <membership defaultProvider="AppSqlMembershipProvider">
      <providers>
        <clear/>
        <add connectionStringName="" name="AppSqlMembershipProvider" type="Spring.Web.Providers.MembershipProviderAdapter, Spring.Web"
             enablePasswordRetrieval="false"
             enablePasswordReset="true"
             requiresQuestionAndAnswer="false"
             requiresUniqueEmail="false"
             passwordFormat="Hashed"
             maxInvalidPasswordAttempts="5"
             minRequiredPasswordLength="7"
             minRequiredNonalphanumericCharacters="0"
             passwordAttemptWindow="10"
             passwordStrengthRegularExpression=""/>
      </providers>
    </membership>

    <roleManager enabled="true" defaultProvider="AppSqlRoleProvider">
      <providers>
        <clear/>
        <add connectionStringName="" name="AppSqlRoleProvider" type="Spring.Web.Providers.RoleProviderAdapter, Spring.Web"/>
      </providers>
    </roleManager>

    <profile defaultProvider="AppSqlProfileProvider">
      <providers>
        <clear/>
        <add name="AppSqlProfileProvider" type="Spring.Web.Providers.ProfileProviderAdapter, Spring.Web" connectionStringName=""/>
      </providers>
      <properties>
        <add name="Greeting" type="String"/>
      </properties>
    </profile>
  </system.web>

</configuration>

AUTH-config.xml中:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">

  <object id="AppSqlMembershipProvider" type="Spring.Web.Providers.ConfigurableSqlMembershipProvider">
    <property name="connectionStringName" value="ApplicationServices" />
    <property name="parameters">
      <name-values>
        <add key="description" value="membershipprovider description" />
      </name-values>
    </property>
  </object>

  <object id="AppSqlRoleProvider" type="Spring.Web.Providers.ConfigurableSqlRoleProvider">
    <property name="connectionStringName" value="ApplicationServices" />
    <property name="parameters">
      <name-values>
        <add key="description" value="roleprovider description" />
      </name-values>
    </property>
  </object>

  <object id="AppSqlProfileProvider" type="Spring.Web.Providers.ConfigurableSqlProfileProvider">
    <property name="connectionStringName" value="ApplicationServices" />
    <property name="parameters">
      <name-values>
        <add key="description" value="profileprovider description" />
      </name-values>
    </property>
  </object>

</objects>

我的MembershipProvider在core-config.xml中配置:

<object id="AccountMembershipService" type="App.Core.Services.AccountMembershipService, App.Core.Services" >
  <constructor-arg name="provider" ref="AppSqlMembershipProvider"/>      <!-- see auth-config.xml for membership provider -->
</object>

正如我之前提到的,我可以在我的网络应用中注入Service没有问题,但我似乎无法让我的测试正常工作:

[TestFixture]
public class AccountMembershipServiceTest 
{
    protected IMembershipService AccountMembershipService { get; set; }   // properyt injected
    [Test]
    public void SanityCheck()
    {
        Assert.IsNotNull(this.AccountMembershipService);            
    }        
}

但是,唉....失败:

------ Test started: Assembly: App.Tests.dll ------

Test 'App.Tests.IntegrationTesting.AccountMembershipServiceTest.SanityCheck' failed: 
  Expected: not null
  But was:  null
    Integration\AccountMembershipServiceTest.cs(23,0): at App.Tests.IntegrationTesting.AccountMembershipServiceTest.SanityCheck()

0 passed, 1 failed, 0 skipped, took 1.80 seconds (NUnit 2.5.5).

其他人遇到同样的问题或者是否已经使用Spring.NET解决了这个问题?

注意:我的单元/集成测试应用程序中的所有其他已配置对象都有效,只有我的成员资格提供程序存在问题。

1 个答案:

答案 0 :(得分:2)

我认为问题出在您的测试代码中,而不是在成员资格配置或Spring.NET上下文配置中。

使用Spring.NET dependency injected tests,您的考试

  • 应该从一个Spring.NET测试基类继承
  • 应该有公共属性注入(使用默认的自动连接类型) - 如果你的属性受到保护,它将为null并且你从你的问题中得到失败的testresult
  • 应指定配置位置

例如:

[TestFixture]
public class AccountMembershipServiceTest : 
             AbstractDependencyInjectionSpringContextTests
{
  // public properties will be auto-wired by type:
  public IMembershipService AccountMembershipService { get; set; }

  protected override string[] ConfigLocations { ... }
}

如果你想要use protected fields for your dependencies,那么你应该使用受保护的字段,而不是属性,并在测试构造函数中设置PopulateProtectedVariables = true

[TestFixture]
public class AccountMembershipServiceTest :
             AbstractDependencyInjectionSpringContextTests
{
    public AccountMembershipServiceTest ()
    {
        PopulateProtectedVariables = true;
    }

    // protected fields will be looked up in the container by field name:
    protected IMembershipService AccountMembershipService;


    protected override string[] ConfigLocations { ... }
}

我怀疑您的会员配置实际上是正确的: