嘗試在測試中創建用戶帳戶。但是運行它時,獲取對象引用不會被設置爲對象錯誤的實例。單元測試具有NUnit的自定義成員資格提供程序會引發空引用錯誤
這裏是我的會員提供者類,它是在類庫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);
}
}
}
這裏是我的App.Config中爲我單獨測試類庫MyCompany.MyApp.Doman.Test.dll引用我的業務領域上面庫:
<?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>
這裏是我的方法拋出「對象引用未設置爲對象的instanve」
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線
但是你在NameValueCollect中傳遞了什麼? – Coppermill 2009-09-10 16:32:24