使用 NUnit 进行单元测试自定义成员资格提供程序会引发空引用错误
尝试在测试中创建用户帐户。 但是,获取对象引用并不会在运行时设置为对象错误的实例。
这是我的 MemberShip 提供程序类,它位于类库 MyCompany.MyApp.Domain.dll 中:
using System;
using System.Collections.Generic;
using System.Web.Security;
namespace MyCompany.MyApp.Domain
{
public class MyMembershipProvider : SqlMembershipProvider
{
const int defaultPasswordLength = 8;
private int resetPasswordLength;
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
resetPasswordLength = defaultPasswordLength;
string resetPasswordLengthConfig = config["resetPasswordLength"];
if (!String.IsNullOrEmpty(resetPasswordLengthConfig))
{
config.Remove("resetPasswordLength");
if (!int.TryParse(resetPasswordLengthConfig, out resetPasswordLength))
{
resetPasswordLength = defaultPasswordLength;
}
}
base.Initialize(name, config);
}
public override string GeneratePassword()
{
return Utils.PasswordGenerator.GeneratePasswordAsWord(resetPasswordLength);
}
}
}
这是我的独立测试类库 MyCompany.MyApp.Doman.Test.dll 的 App.Config,它引用了上面的业务域库:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="SqlServer" connectionString="data source=mycomp\SQL2008;Integrated Security=SSPI;Initial Catalog=myDatabase" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<membership defaultProvider="MyMembershipProvider" userIsOnlineTimeWindow="15">
<providers>
<clear/>
<add name="MyMembershipProvider"
type="MyCompany.MyApp.Domain.MyMembershipProvider,MyCompany.MyApp.Domain"
connectionStringName="SqlServer"
applicationName="MyApp"
minRequiredNonalphanumericCharacters="0"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="true"
passwordFormat="Hashed"/>
</providers>
</membership>
</system.web>
</configuration>
这是我的方法抛出“对象引用未设置为对象的实例”,
public class MemberTest
{
public static void CreateAdminMemberIfNotExists()
{
MembershipCreateStatus status;
status = MembershipCreateStatus.ProviderError;
MyMembershipProvider provider = new MyMembershipProvider();
provider.CreateUser("Admin", "password", "[email protected]", "Question", "Answer", true, Guid.NewGuid(), out status);
}
}
它在provider.CreateUser行上抛出
Trying to create a user account in a test. But getting a Object reference is not set to an instanve of an object error when running it.
Here's my MemberShip provider class, it's in a class library MyCompany.MyApp.Domain.dll:
using System;
using System.Collections.Generic;
using System.Web.Security;
namespace MyCompany.MyApp.Domain
{
public class MyMembershipProvider : SqlMembershipProvider
{
const int defaultPasswordLength = 8;
private int resetPasswordLength;
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
resetPasswordLength = defaultPasswordLength;
string resetPasswordLengthConfig = config["resetPasswordLength"];
if (!String.IsNullOrEmpty(resetPasswordLengthConfig))
{
config.Remove("resetPasswordLength");
if (!int.TryParse(resetPasswordLengthConfig, out resetPasswordLength))
{
resetPasswordLength = defaultPasswordLength;
}
}
base.Initialize(name, config);
}
public override string GeneratePassword()
{
return Utils.PasswordGenerator.GeneratePasswordAsWord(resetPasswordLength);
}
}
}
Here's my App.Config for my seperate Test Class Library MyCompany.MyApp.Doman.Test.dll that references my business domain library above:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="SqlServer" connectionString="data source=mycomp\SQL2008;Integrated Security=SSPI;Initial Catalog=myDatabase" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<membership defaultProvider="MyMembershipProvider" userIsOnlineTimeWindow="15">
<providers>
<clear/>
<add name="MyMembershipProvider"
type="MyCompany.MyApp.Domain.MyMembershipProvider,MyCompany.MyApp.Domain"
connectionStringName="SqlServer"
applicationName="MyApp"
minRequiredNonalphanumericCharacters="0"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="true"
passwordFormat="Hashed"/>
</providers>
</membership>
</system.web>
</configuration>
Here's my method that throws "Object reference is not set to an instanve of an object"
public class MemberTest
{
public static void CreateAdminMemberIfNotExists()
{
MembershipCreateStatus status;
status = MembershipCreateStatus.ProviderError;
MyMembershipProvider provider = new MyMembershipProvider();
provider.CreateUser("Admin", "password", "[email protected]", "Question", "Answer", true, Guid.NewGuid(), out status);
}
}
it throws on the provider.CreateUser line
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我确信您应该在调用 CreateUser 之前在测试代码中调用provider.Initialize(...)。
I'm quite sure you should call provider.Initialize(...) in your test code before calling CreateUser.
好吧,并不是所有人都有答案。 尽管初始化是问题所在,但 csgero 走在正确的轨道上。 但直接调用并不是一个解决方案。 这是有效的:
我相信直接实例化我的会员提供程序需要设置通常存储在 app.config 或 web.config 中的配置属性,然后调用初始化。 然而,调用 Mebership 类上的静态 CreateUser 方法会导致读取配置,然后解析、加载和初始化配置中指定的类型。
Well not quite anyone had the answer. csgero was on the right track though initialize was the problem. But just calling that directly was not a solution. This works:
I believe instantiating my membership provider directly requires setting the config properties typically stored in the app.config or web.config and then calling initialize. Calling the static CreateUser method on the Mebership class however causes the config to be read, then the type specified in the config is parsed, loaded and initialised.
错误是直接出现在provider.CreateUser上还是位于堆栈内部的某个位置 - 也许您可以在调用之前检查是否为null。
也许缺少依赖项 - 您在路径上是否有相关的 DB dll?
Is the error directly on provider.CreateUser or somewhere down the stack inside it - perhaps you could check for null before calling.
Perhaps a dependancy is missing - have you got the relevant DB dll's on the path?