获取 Moq 以调用未被覆盖的基本 class 方法
Get Moq to call a base class method that has not been overriden
我在我的 MVC 应用程序中使用 Asp.Net Identity,我有一个名为 ApplicationSignInManager
的 class,如下所示:
public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
public ApplicationSignInManager(ApplicationUserManager userManager,
IAuthenticationManager authenticationManager)
: base(userManager, authenticationManager)
{
}
public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
{
return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
}
public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options,
IOwinContext context)
{
return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(),
context.Authentication);
}
}
现在,我想为控制器上的操作编写单元测试。我正在使用带有 Moq 4.5.10 的 Microsoft Visual Studio 单元测试框架。
该操作调用 PasswordSignInAsync
在基 class SignInManager<ApplicationUser, string>
中声明的 virtual
方法,但未在子 class 中覆盖它 ApplicationSignInManager
.
我也尝试过这个选项:
var mockSignInManager = new Mock<ApplicationSignInManager>()
{ CallBase = true };
mockSignInManager.Setup(
m => m.PasswordSignInAsync(string.Empty, string.Empty, true, true));
但是 PasswordSignInAsync
没有出现在 Intellisense 中,编译器抱怨它无法在 ApplicationSignInManager
上找到该方法。
如何让它显示出来?
我在单元测试项目中安装了 NuGet 包 Microsoft.AspNet.Identity.Owin,它开始获取基础 class 中的所有方法,因为该基础 class 在 Microsoft.AspNet.Identity.Owin.dll 程序集中。
简而言之,如果您想在模拟对象中访问基 class 的方法,那么如果该基 class 位于与派生 class 分开的程序集中,您必须在测试项目中添加对基础 class 程序集的引用。
我在我的 MVC 应用程序中使用 Asp.Net Identity,我有一个名为 ApplicationSignInManager
的 class,如下所示:
public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
public ApplicationSignInManager(ApplicationUserManager userManager,
IAuthenticationManager authenticationManager)
: base(userManager, authenticationManager)
{
}
public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
{
return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
}
public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options,
IOwinContext context)
{
return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(),
context.Authentication);
}
}
现在,我想为控制器上的操作编写单元测试。我正在使用带有 Moq 4.5.10 的 Microsoft Visual Studio 单元测试框架。
该操作调用 PasswordSignInAsync
在基 class SignInManager<ApplicationUser, string>
中声明的 virtual
方法,但未在子 class 中覆盖它 ApplicationSignInManager
.
我也尝试过这个选项:
var mockSignInManager = new Mock<ApplicationSignInManager>()
{ CallBase = true };
mockSignInManager.Setup(
m => m.PasswordSignInAsync(string.Empty, string.Empty, true, true));
但是 PasswordSignInAsync
没有出现在 Intellisense 中,编译器抱怨它无法在 ApplicationSignInManager
上找到该方法。
如何让它显示出来?
我在单元测试项目中安装了 NuGet 包 Microsoft.AspNet.Identity.Owin,它开始获取基础 class 中的所有方法,因为该基础 class 在 Microsoft.AspNet.Identity.Owin.dll 程序集中。
简而言之,如果您想在模拟对象中访问基 class 的方法,那么如果该基 class 位于与派生 class 分开的程序集中,您必须在测试项目中添加对基础 class 程序集的引用。