【问题标题】:MOQ return new object from method with parametersMOQ 从带有参数的方法返回新对象
【发布时间】:2016-11-11 21:33:47
【问题描述】:

在这方面有点麻烦。我第一次尝试使用起订量。

我有一个界面:

public interface IUserService
{
    void SignOut(string username);
    AuthenticationResult Authenticate(string username, string password);
    Models.Users.ApplicationUser GetAvailibleUserDetails(string username);
    List<ApplicationUser> GetAvailibleUsers();
    List<ApplicationUser> GetApplicationUsers();
    ApplicationUser GetUser(Expression<Func<ApplicationUser, bool>> filter);
    ApplicationUser AddUser(string username, List<string> environmentIds, bool isAdmin =false);
    ApplicationUser UpdateUser(string id, List<string> environmentIds, bool isAdmin = false);
    void DeleteUser(string id);
}

使用 System.DirectoryServices.AccountManagement 库进行 AD 身份验证的 Authenticate 方法。

这里是方法的实现:

public AuthenticationResult Authenticate(string username, string password)
    {
        UserPrincipal userPrincipal = null;

        var isAuthenticated = _adManager.ValidateUser(username, password);

        if (isAuthenticated)
        {
            userPrincipal = _adManager.GetUserPrincipal(username);
        }


        if (!isAuthenticated || userPrincipal == null)
        {
            return new AuthenticationResult("Username or Password is not correct");
        }

        if (userPrincipal.IsAccountLockedOut())
        {
            // here can be a security related discussion weather it is worth 
            // revealing this information
            return new AuthenticationResult("Your account is locked.");
        }

        if (userPrincipal.Enabled.HasValue && userPrincipal.Enabled.Value == false)
        {
            // here can be a security related discussion weather it is worth 
            // revealing this information
            return new AuthenticationResult("Your account is disabled");
        }

        //Determine if the user is authorized to use the application
        var user = _userManager.GetUser(u => u.LdapId == userPrincipal.Sid.Value);
        if (user == null)
        {
            return new AuthenticationResult("You are not authorized to use this application");
        }

        var identity = CreateIdentity(userPrincipal, user);

        _authenticationManager.SignOut("MAG.EPM");
        _authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity);


        return new AuthenticationResult();
    }

这是我的测试类和方法:

public class UserServiceTest
{
    private readonly Mock<IActiveDirectoryManager> _adManager;
    private readonly Mock<UserPrincipalWrapper> _userPrincipal;
    private readonly Mock<IUserManager> _userManager;
    private readonly Mock<IAuthenticationManager> _authenticationManager;

    public UserServiceTest()
    {
        _userPrincipal = new Mock<UserPrincipalWrapper>();
        _userManager = new Mock<IUserManager>();
        _adManager = new Mock<IActiveDirectoryManager>();
        _authenticationManager = new Mock<IAuthenticationManager>();
    }

    [TestMethod]
    public void Authenticate_ValidActiveDirectoryUser_Authenticated()
    {
        _userPrincipal.Setup(x => x.IsAccountLockedOut()).Returns(false);
        _userPrincipal.SetupGet(x => x.Enabled).Returns(true);


        _adManager.Setup(x => x.ValidateUser("testUser", "password")).Returns(true);
        _adManager.Setup(x => x.GetUserPrincipal("testUser")).Returns(_userPrincipal.Object);

        _userManager.Setup(x => x.GetUser(u => u.LdapId == Guid.NewGuid().ToString())).Returns(new ApplicationUser());

        IUserService userService = new UserService(_userManager.Object, _authenticationManager.Object, _adManager.Object);


        Assert.IsTrue(userService.Authenticate("testUser", "password").IsSuccess);

    }

}

问题是我在从

返回的用户对象上获得了一个空引用
//Determine if the user is authorized to use the application
        var user = _userManager.GetUser(u => u.LdapId == userPrincipal.Sid.Value);
        if (user == null)
        {
            return new AuthenticationResult("You are not authorized to use this application");
        }

根据我的设置 - 它应该只返回一个 new ApplicationUser()

其他一切正常。

IUserManager 界面如下所示:

    public interface IUserManager
{
    List<ApplicationUser> GetUsers(Expression<Func<ApplicationUser, bool>> filter = null);
    ApplicationUser GetUser(Expression<Func<ApplicationUser, bool>> filter);
    ApplicationUser AddUser(string username, List<string> environmentIds, bool isAdmin = false);
    void DeleteUser(string username);
    ApplicationUser UpdateUser(string id, List<string> environmentIds, bool isAdmin = false);
    IList<string> GetUserRoles(string id);


}

所以对于_userManager.Setup(u =&gt; u.LdapId == Guid.NewGuid().tostring())).Returns(new ApplicationUser());

我正在尝试替换 _userManager.GetUser(u =&gt; u.LdapId == userPrincipal.Sid.Value);

我确实尝试用

替换测试方法
_userManager.Setup(x => x.GetUser(u => u.LdapId == _userPrincipal.Object.Sid.Value)).Returns(new ApplicationUser());

匹配实现中的调用,但我在 _userPrincipal 对象上得到一个空引用异常(“对象未设置...”)。

【问题讨论】:

    标签: c# .net nunit moq userprincipal


    【解决方案1】:

    试试这样:

    userService
        .Setup(s => s.GetUser(It.IsAny<Expression<Func<ApplicationUser, bool>>>()))
        .Returns(new ApplicationUser());
    

    【讨论】:

    • 太棒了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 2016-02-09
    • 2020-03-03
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多