2016-07-19 15 views
3

私はこのコントローラメソッドを単体テストしようとしています。このコントローラメソッドは、現在のMVCプロジェクトでそのまま使用できます。MVC5のConfirmEmailAsyncと他のUserManagerメソッドをモックするためのインタフェース

[AllowAnonymous] 
public async Task<ActionResult> ConfirmEmail(string userId, string code) 
{ 
    if (userId == null || code == null) 
    { 
     return View("Error"); 
    } 
    var result = await UserManager.ConfirmEmailAsync(userId, code); 
    return View(result.Succeeded ? "ConfirmEmail" : "Error"); 
} 

AccountControllerをパラメータとしてApplicationUserManagerとApplicationSignInManagerを取るコンストラクタを有し、プライベートセッターとのマッチング特性は、試験のために使用します。しかし、私はConfirmEmailAsyncメソッドを模擬する方法を理解できません。

あなたはアイデンティティの名前空間でさまざまなインターフェイス模擬することができます

var store = new Mock<IUserStore<ApplicationUser>>(); 

store.As<IUserEmailStore<ApplicationUser>>() 
      .Setup(x => x.FindByIdAsync("username1")) 
      .ReturnsAsync((ApplicationUser)null); 

var mockManager = new ApplicationUserManager(store.Object); 

AccountController ac = new AccountController(mockManager, null, GetMockRepository().Object, GetMockLogger().Object); 

をしかし、私はConfirmEmailAsyncのモックを作成するために必要があるインターフェイスを見つけるか把握することはできません。

どうすればよいですか?参考までに、これらのメソッドが偽装してテストするためにどのインタフェースが使用されているかを調べる良い方法はありますか?

+0

私は単体テストを容易にし、他のプロジェクトで抽象化を再利用できるように、アイデンティティの機能の大部分を独自のプロジェクトに抽象化してこの問題を回避しました。私はこの記事http://timschreiber.com/2015/01/14/persistence-ignorant-asp-net-identity-with-patterns-part-1/から始め、自分のニーズに合わせて微調整しました – Nkosi

答えて

2

ConfirmEmailAsyncは現在、フレームワークのインターフェイスの一部ではありません。 Identityフレームワークの基本クラスであるUserManager<TUser, TKey>クラスにあります。

私のソリューションですか?

抽象私はユニットより簡単にそれをテストし、他のプロジェクトで抽象化を再利用できるように、私は、独自のプロジェクトにアイデンティティのほとんどの機能を抽象化することにより、この問題を回避得たすべてのもの

。私は自分のニーズに合わせて、私はその後、微調整この記事

Persistence-Ignorant ASP.NET Identity with Patterns

のアイデアを読んだ後にアイデアを得ました。私は基本的に、フレームワークが提供する機能を多かれ少なかれミラーリングしたが、より簡単なモックアビリティの利点を備えた私のカスタムインターフェイスには、asp.net.identityから必要なものすべてを入れ替えただけです。私は単にApplicationManagerを包み、その後、私のタイプの間の結果と機能性をマッピングされたアイデンティティ管理の私のデフォルトの実装では

IIdentityUser

/// <summary> 
/// Minimal interface for a user with an id of type <seealso cref="System.String"/> 
/// </summary> 
public interface IIdentityUser : IIdentityUser<string> { } 
/// <summary> 
/// Minimal interface for a user 
/// </summary> 
public interface IIdentityUser<TKey> 
    where TKey : System.IEquatable<TKey> { 

    TKey Id { get; set; } 
    string UserName { get; set; } 
    string Email { get; set; } 
    bool EmailConfirmed { get; set; } 
    string EmailConfirmationToken { get; set; } 
    string ResetPasswordToken { get; set; } 
    string PasswordHash { get; set; } 
} 

IIdentityManager

/// <summary> 
/// Exposes user related api which will automatically save changes to the UserStore 
/// </summary> 
public interface IIdentityManager : IIdentityManager<IIdentityUser> { } 
/// <summary> 
/// Exposes user related api which will automatically save changes to the UserStore 
/// </summary> 
public interface IIdentityManager<TUser> : IIdentityManager<TUser, string> 
    where TUser : class, IIdentityUser<string> { } 
/// <summary> 
/// Exposes user related api which will automatically save changes to the UserStore 
/// </summary> 
public interface IIdentityManager<TUser, TKey> : IDisposable 
    where TUser : class, IIdentityUser<TKey> 
    where TKey : System.IEquatable<TKey> { 

    Task<IIdentityResult> AddPasswordAsync(TKey userid, string password); 
    Task<IIdentityResult> ChangePasswordAsync(TKey userid, string currentPassword, string newPassword); 
    Task<IIdentityResult> ConfirmEmailAsync(TKey userId, string token); 
    //...other code removed for brevity 
} 

IIdentityResult

/// <summary> 
/// Represents the minimal result of an identity operation 
/// </summary> 
public interface IIdentityResult : System.Collections.Generic.IEnumerable<string> { 
    bool Succeeded { get; } 
} 

asp.net.identity tyペス。

public class DefaultUserManager : IIdentityManager { 
    private ApplicationUserManager innerManager; 

    public DefaultUserManager() { 
     this.innerManager = ApplicationUserManager.Instance; 
    } 
    //..other code removed for brevity 
    public async Task<IIdentityResult> ConfirmEmailAsync(string userId, string token) { 
     var result = await innerManager.ConfirmEmailAsync(userId, token); 
     return result.AsIIdentityResult(); 
    } 
    //...other code removed for brevity 
} 
0

免責事項:私はTypemockで働いています。

Typemockを使用している場合は、実際にはインターフェイスは必要ありません。必要なIdentityResultを偽装して非同期メソッド "ConfirmEmailAsync"の動作を変更するだけです。たとえば、未確認のシナリオをチェックするテスト電子メール:

関連する問題