测试 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 解决了这个问题吗?
注意:我的单元/集成测试应用程序中的所有其他配置对象都有效,只有我的会员提供程序出现问题。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题出在您的测试代码中,而不是在成员资格配置或 Spring.NET 上下文配置中。
使用 Spring.NET 依赖注入测试,您的测试
应指定配置 实例:
如果你想使用受保护的字段对于您的依赖项,那么您应该使用受保护的字段,而不是属性,并在测试构造函数中设置
PopulateProtectedVariables = true
:我怀疑您的成员资格配置实际上是正确的:
app .config
到您的单元测试项目带有<会员资格 ...>...
部分type=""
配置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
For instance:
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:I suspect your membership configuration is actually correct:
app.config
to your unit test project with a<system.web><membership ...>...</system.web>
sectiontype=""
configuration