我尝试在Microsoft.AspNet.Identity.Core上实现注册库。
package.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.3" targetFramework="net451" />
<package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net451" />
<package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.1" targetFramework="net451" />
<package id="Microsoft.AspNet.Identity.Owin" version="2.2.1" targetFramework="net451" />
<package id="Microsoft.Owin" version="3.0.1" targetFramework="net451" />
<package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net451" />
<package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net451" />
<package id="Microsoft.Owin.Security.OAuth" version="3.0.1" targetFramework="net451" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net451" />
<package id="Owin" version="1.0" targetFramework="net451" />
</packages>
我的单元测试班:
public class register_test
{
Mock<IUserStore<ApplicationUser>> _userStore;
IRegisterService _registerService;
Mock<ApplicationUserManager> _userManagerMock;
IDataProtectionProvider _dataProvider;
public register_test()
{
_dataProvider = new DpapiDataProtectionProvider("paracours");
_userStore = new Mock<IUserStore<ApplicationUser>>();
_userManagerMock = new Mock<ApplicationUserManager>(_userStore.Object, _dataProvider);
_registerService = new RegisterService(_userManagerMock.Object);
}
[Fact]
public async Task register_sucess()
{
ApplicationUser user = new ApplicationUser() { Email = "user1@test.fr", UserName = "user1@test.fr" };
_userManagerMock.Setup(u => u.CreateAsync(It.IsAny<ApplicationUser>(), It.IsAny<string>()))
.ReturnsAsync(IdentityResult.Success)
.Callback(() => user.Id = "0f8fad5b-d9cb-469f-a165-70867728950e");
var result = await _registerService.RegisterAsync(user);
_userManagerMock.Verify(x =>
x.CreateAsync(
It.Is<ApplicationUser>(u => u.Email == "user1@test.fr"),
It.Is<string>(pass => pass == "P@ssword1")));
Assert.NotNull(result);
Assert.Equal(user.Id, "0f8fad5b-d9cb-469f-a165-70867728950e");
}
[Fact]
public void email_token_generation_success()
{
_userManagerMock.Setup(u => u.FindByIdAsync(It.IsAny<string>()))
.ReturnsAsync(new ApplicationUser() { Email = "user1@test.fr", UserName = "user1@test.fr", EmailConfirmed = false });
var result = _registerService.EmailToken("0f8fad5b-d9cb-469f-a165-70867728950e");
Assert.NotNull(result);
}
}
我的服务:
public class RegisterService : IRegisterService
{
private readonly ApplicationUserManager _userManager;
public RegisterService() { }
public RegisterService(ApplicationUserManager userManager)
{
_userManager = userManager;
}
public virtual async Task<IdentityResult> RegisterAsync(ApplicationUser user)
{
return await _userManager.CreateAsync(user, "P@ssword1");
}
public virtual string EmailToken(string userId)
{
return _userManager.GenerateEmailConfirmationToken(userId);
}
}
我的调试配置:
msdl.microsoft.com/download/symbols
我这样做:
提出一个断点:
public virtual string EmailToken(string userId){ return _userManager.GenerateEmailConfirmationToken(userId); }
当我触摸F11时,它会转到:
using System;
using System.Collections.Generic;
using System.Security.Claims;
namespace Microsoft.AspNet.Identity
{
/// <summary>
/// Extension methods for UserManager
/// </summary>
public static class UserManagerExtensions
{
...
/// <summary>
/// Get the confirmation token for the user
/// </summary>
/// <param name="manager"></param>
/// <param name="userId"></param>
/// <returns></returns>
public static string GenerateEmailConfirmationToken<TUser, TKey>(this UserManager<TUser, TKey> manager,
TKey userId)
where TKey : IEquatable<TKey>
where TUser : class, IUser<TKey>
{
if (manager == null)
{
throw new ArgumentNullException("manager");
}
return AsyncHelper.RunSync(() => manager.GenerateEmailConfirmationTokenAsync(userId));
}
...
}
}
我不知道如何调试:
manager.GenerateEmailConfirmationTokenAsync(用户id)
内
AsyncHelper.RunSync(()=&gt;
我需要保留,这对我来说是新的任务和调试
答案 0 :(得分:1)
我解决了自己的问题。 当我使用:
模拟ApplicationUserManager时new Mock(_userStore.Object,_dataProvider);
为GenerateEmailConfirmationToken =&gt;创建一个Castle.Proxie无法在符号(.pdb)中解析
解决方案是不要模拟ApplicationUserManager