Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 最低起订量元';t调用IAAuthenticationManager登录方法_C#_Asp.net Mvc_Unit Testing_Asp.net Identity_Moq - Fatal编程技术网

C# 最低起订量元';t调用IAAuthenticationManager登录方法

C# 最低起订量元';t调用IAAuthenticationManager登录方法,c#,asp.net-mvc,unit-testing,asp.net-identity,moq,C#,Asp.net Mvc,Unit Testing,Asp.net Identity,Moq,我正在尝试通过验证AuthenticationManager SignIn方法是否已被调用来运行成功登录的单元测试。下面是我收到的错误: 消息:测试方法Portfolio.UnitTest.WebUI.Controllers.LoginControllerTests.Login\u Verified\u Authenticates\u System\u用户引发异常: 最小起订量例外情况: 应在模拟上调用一次,但调用次数为0次:m=>m.SignIn(AuthenticationPropertie

我正在尝试通过验证AuthenticationManager SignIn方法是否已被调用来运行成功登录的单元测试。下面是我收到的错误:

消息:测试方法Portfolio.UnitTest.WebUI.Controllers.LoginControllerTests.Login\u Verified\u Authenticates\u System\u用户引发异常: 最小起订量例外情况: 应在模拟上调用一次,但调用次数为0次:m=>m.SignIn(AuthenticationProperties,[ClaimSideEntity])

配置的设置: m=>m.SignIn(AuthenticationProperties,[ClaimSideEntity])

执行的调用: IAAuthenticationManager.SignIn(AuthenticationProperties,[ClaimsEntity])

甚至错误信息似乎也自相矛盾

我的控制器类/方法:

public class LoginController : ProjectBaseController
{
private IAuthenticationManager _authenticationManager;
    public IAuthenticationManager AuthenticationManager
    {
        get
        {
            return _authenticationManager ?? HttpContext.GetOwinContext().Authentication;
        }
        set
        {
            _authenticationManager = value;
        }
    }
[HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Index(LoginViewModel accountUser)
    {
        if (!ModelState.IsValid)
            return View(accountUser);

        var systemUser = _authenticationService.Verify(accountUser.Email, accountUser.Password);
        if (systemUser == null)
        {
            ModelState.AddModelError("", "Invalid email address or password");
            return View(accountUser);
        }

        var identity = new ClaimsIdentity
        (
            new[] 
            {
                new Claim(ClaimTypes.Name, systemUser.FullName),
                new Claim(ClaimTypes.Email, systemUser.Email)
            },
            DefaultAuthenticationTypes.ApplicationCookie,
            ClaimTypes.Name, 
            ClaimTypes.Role
        );

        // Set roles for authorization attributes used throughout dashboard
        foreach(var role in systemUser.Roles)
        {
            identity.AddClaim(new Claim(ClaimTypes.Role, role.Name));
        }

        AuthenticationManager.SignIn
        (
            new AuthenticationProperties
            {
                IsPersistent = true
            }, 
            identity
        );

        return Redirect(accountUser.ReturnUrl);   
    }
登录控制器测试:

[TestClass]
public class LoginControllerTests
{
    private readonly Mock<IAuthenticationManager> _mockAuthenticationManager;
    private readonly Mock<IAuthenticationService> _mockAuthenticationService;

    public LoginControllerTests()
    {
        _mockAuthenticationManager = new Mock<IAuthenticationManager>();
        _mockAuthenticationService = new Mock<IAuthenticationService>();
    }

    private LoginController GetLoginControllerInstance()
    {
        var controller = new LoginController(_mockAuthenticationService.Object);
        controller.AuthenticationManager = _mockAuthenticationManager.Object;

        return controller;
    }

    [TestMethod]
    public void Login_Verified_Authenticates_System_User()
    {
        // Arrange
        var viewModel = new LoginViewModel("/")
        {
            Email = "email@test.co.uk",
            Password = "password-test"
        };
        var systemUser = new SystemUser()
        {
            Id = new Random().Next(),
            FirstName = "Joe",
            LastName = "Bloggs",
            Email = viewModel.Email,
            Password = viewModel.Password,
            Roles = new List<Role>()
        };
        var identity = new ClaimsIdentity
        (
            new[]
            {
                new Claim(ClaimTypes.Name, systemUser.FullName),
                new Claim(ClaimTypes.Email, systemUser.Email)
            },
            DefaultAuthenticationTypes.ApplicationCookie,
            ClaimTypes.Name,
            ClaimTypes.Role
        );
        var authenticationProperty = new AuthenticationProperties
        {
            IsPersistent = true
        };

        var controller = this.GetLoginControllerInstance();
        _mockAuthenticationService.Setup(m => m.Verify(viewModel.Email, viewModel.Password))
            .Returns(systemUser);
        _mockAuthenticationManager.Setup(m => m.SignIn(authenticationProperty, identity));

        // Act
        var result = controller.Index(viewModel);

        // Assert
        Assert.IsNotNull(result);
        Assert.IsTrue(controller.ModelState.IsValid);
        _mockAuthenticationService.Verify(m => m.Verify(viewModel.Email, viewModel.Password), Times.Once);
        _mockAuthenticationManager.Verify(m => m.SignIn(authenticationProperty, identity), Times.Once);
    }
}
}
[TestClass]
公共类登录控件测试
{
私有只读Mock\u mockAuthenticationManager;
私有只读Mock\u mockAuthenticationService;
公共登录控制器测试()
{
_mockAuthenticationManager=新建Mock();
_mockAuthenticationService=新建Mock();
}
私有LoginController GetLoginControllerInstance()
{
var controller=new LoginController(\u mockAuthenticationService.Object);
controller.AuthenticationManager=\u mockAuthenticationManager.Object;
返回控制器;
}
[测试方法]
公共无效登录\u已验证\u身份验证\u系统\u用户()
{
//安排
var viewModel=new LoginViewModel(“/”)
{
电子邮件=”email@test.co.uk",
Password=“密码测试”
};
var systemUser=new systemUser()
{
Id=new Random().Next(),
FirstName=“乔”,
LastName=“Bloggs”,
Email=viewModel.Email,
Password=viewModel.Password,
角色=新列表()
};
var标识=新的索赔实体
(
新[]
{
新索赔(ClaimTypes.Name、systemUser.FullName),
新索赔(ClaimTypes.Email、systemUser.Email)
},
DefaultAuthenticationTypes.ApplicationOkie,
索赔类型。名称,
索赔类型。角色
);
var authenticationProperty=新的AuthenticationProperties
{
ispersist=true
};
var controller=this.GetLoginControllerInstance();
_mockAuthenticationService.Setup(m=>m.Verify(viewModel.Email、viewModel.Password))
.返回(系统用户);
_mockAuthenticationManager.Setup(m=>m.SignIn(authenticationProperty,identity));
//表演
var结果=控制器索引(viewModel);
//断言
Assert.IsNotNull(结果);
IsTrue(controller.ModelState.IsValid);
_mockAuthenticationService.Verify(m=>m.Verify(viewModel.Email,viewModel.Password),Times.Once);
_mockAuthenticationManager.Verify(m=>m.SignIn(authenticationProperty,identity),Times.Once);
}
}
}
在deubg中运行时,该方法在控制器中被调用,但Moq似乎忽略了它


有什么想法吗?

恩科西在对你的问题的评论中说:

在表达式中

m => m.SignIn(authenticationProperty, identity)
两个参数
authenticationProperty
identity
必须“等于”实际传入的两个参数(由被测系统传入)。那么当比较时,
Equals
是否返回true


如果需要,可以使用
It.IsAny()
It.Is((AuthenticationProperties x)=>x.IsPersistent)
或类似工具。与
索赔类似
。然后Moq将不再要求
相等
相等。

这些是测试方法和实际测试方法中使用的不同实例。这就是为什么它无法验证的原因。它的复制品就这么简单!很明显,我对这一切都不熟悉。Thanks@user3112059因为你对模仿还不熟悉,你可能想看看关于它的使用的一些讨论。