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

发布于 2024-12-29 03:16:09 字数 5539 浏览 2 评论 0原文

喜欢注入会员资格提供商的灵活性,但我正在绞尽脑汁试图让集成测试通过。我的 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 解决了这个问题吗?

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

Loving the flexibility injecting the membership provider but I'm scratching my head trying to get integration tests to pass. My web application version injects the membership provider with no problem in my service layer, but I can't get it configured/working properly in my unit/integration testing application version. Here is how I have everything configured for the membership provider.

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>

My MembershipProvider is configured in 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>

As I mentioned before, I can inject the Service no problem in my web app but I can't seem to get my test to work correctly:

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

But, alas....failure:

------ 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).

Anyone else having the same problem or has tackled this as well with Spring.NET?

NOTE: All of my other configured objects in my unit/integration testing application works, only my membership provider is being problematic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说不完的你爱 2025-01-05 03:16:09

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

使用 Spring.NET 依赖注入测试,您的测试

  • 应该继承自 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 { ... }
}

如果你想使用受保护的字段对于您的依赖项,那么您应该使用受保护的字段,而不是属性,并在测试构造函数中设置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 { ... }
}

我怀疑您的成员资格配置实际上是正确的:

I think the problem is in your test code and not in the membership configuration or Spring.NET context configuration.

To use Spring.NET dependency injected tests, your test

  • should inherit from one of the Spring.NET test base classes
  • should have public properties to inject into (using the default auto-wire by type) - if your property is protected, it'll be null and you get the failed testresult from your question
  • should specify the config locations

For instance:

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

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

If you want to use protected fields for your dependencies, then you should use protected fields, not properties and set PopulateProtectedVariables = true in your test constructor:

[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 { ... }
}

I suspect your membership configuration is actually correct:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文