集成测试:MembershipProvider - TypeLoadException

时间:2013-01-14 18:26:40

标签: asp.net tdd asp.net-membership

我试图编写一些使用CustomMembershipProvider创建/删除/更改用户的测试。 CustomMembershipProvider是WebApp程序集的一部分。

当我运行测试时,我得到以下异常:

System.TypeLoadException : Could not load type 'WebApp.Framework.CustomMembershipProvider' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, ref StackCrawlMark stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
   at System.Type.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase)
   at System.Web.Security.Membership.InitializeSettings(Boolean initializeGeneralSettings, RuntimeConfig appConfig, MembershipSection settings)
   at System.Web.Security.Membership.Initialize()
   at System.Web.Security.Membership.get_Providers()
   at System.Web.Security.MembershipUser..ctor(String providerName, String name, Object providerUserKey, String email, String passwordQuestion, String comment, Boolean isApproved, Boolean isLockedOut, DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate, DateTime lastPasswordChangedDate, DateTime lastLockoutDate)
   at BusinessLayer.Users.UserRepository.Add(User user, ref MembershipCreateStatus status) in UserRepository.cs: line 43
   at WebApp.Framework.CustomMembershipProvider.CreateUser(String username, String password, String email, String passwordQuestion, String passwordAnswer, Boolean isApproved, Object providerUserKey, ref MembershipCreateStatus status) in CustomMembershipProvider.cs: line 86
   at Tests.Framework.CustomMembershipProviderTests.CreateUser() in CustomMembershipProviderTests.cs: line 22

执行这行代码时抛出异常:

        return new MembershipUser(
            "MyMembershipProvider",
            user.Name,
            null,
            user.EmailAddress,
            string.Empty,
            string.Empty,
            true,
            false,
            DateTime.MinValue,
            DateTime.MinValue,
            DateTime.MinValue,
            DateTime.MinValue,
            DateTime.MinValue);

在我的测试项目中,我的app.conf中添加了以下行:

会员定义:

  <system.web>
    <membership defaultProvider="MyMembershipProvider">
      <providers>
        <clear/>
        <add name="MyMembershipProvider" type="WebApp.Framework.CustomMembershipProvider" connectionStringName="DefaultConnection"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>
  </system.web>

数据库连接:

  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=WS050129\SQLEXPRESS;Initial Catalog=Users;Integrated Security=True;" providerName="System.Data.SqlClient" />
  </connectionStrings>

对我来说看起来不错,但显然有些东西已经关闭:)。有人能指出我正确的方向吗?有点无能为力。

非常感谢。

1 个答案:

答案 0 :(得分:9)

尝试更改此内容:

<add name="MyMembershipProvider" type="WebApp.Framework.CustomMembershipProvider"       connectionStringName="DefaultConnection"
         enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
         maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
         applicationName="/" />

......对此:

<add name="MyMembershipProvider" type="WebApp.Framework.CustomMembershipProvider, WebApp" connectionStringName="DefaultConnection"
         enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
         maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
         applicationName="/" />

您可能需要稍微调整一下。但看起来问题是,缺乏有关该类型的汇编的信息,框架正在查看System.Web。充实该标记上的类型定义属性。