2008-10-09 36 views
0

嘗試在測試中創建用戶帳戶。但是運行它時,獲取對象引用不會被設置爲對象錯誤的實例。單元測試具有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線

回答

2

我很確定在調用CreateUser之前,應該在測試代碼中調用provider.Initialize(...)。

+0

但是你在NameValueCollect中傳遞了什麼? – Coppermill 2009-09-10 16:32:24

0

直接在provider.CreateUser或向下堆棧裏面的某處錯誤 - 也許你可以調用之前檢查空。

也許依賴缺失 - 你有相關的數據庫DLL的路徑?

1

好吧,沒有任何人有答案。雖然初始化是問題,但csgero仍處於正確的軌道上。但直接調用並不是一個解決方案。這工作:

public class MemberTest 
    { 
     public static void CreateAdminMemberIfNotExists() 
     { 
      MembershipCreateStatus status; 
      MembershipUser member = Membership.CreateUser("Admin", "password", "[email protected]", "Question", "Answer", true, out status); 
     } 
    } 

我相信我的實例成員提供直接要求設置通常存儲在App.config或web.config中的配置屬性,然後調用初始化。在Mebership類中調用靜態CreateUser方法會導致配置被讀取,然後在config中指定的類型被解析,加載並初始化。

相關問題