小编kuz*_*agi的帖子

如何使用在方法中具有依赖关系的moq对类进行单元测试?

我想对一个用于验证用户的类AuthClient(使用Moq)进行单元测试.

问题是我的AuthenticationService依赖关系没有注入课堂.我对如何在不注入依赖项的情况下测试流程感到困惑.

[TestClass] 
public class AuthClient 
{
    string Dictionary<string, IList<UserPermissions> _userPermissions;

    User Login(string username, string password)
    {
        IAuthenticationService authenticationService = new AuthenticationService();
        User user = authService.Login(username, password);

        IAuthorizationService authorizationService = new AuthorizationService();
        var permissions = authorizationService.GetPermissions(user);
        _userPermissions.Add(user.Username, permissions);

        return user;
    }
}
Run Code Online (Sandbox Code Playgroud)

我目前的单元测试代码如下:

public class UnitTestClass
{
    public string UserName {get;set;}
    public string Password {get;set;}

    [TestInitialize]
    public void Setup() 
    {
        UserName = "test";
        Password = "test123";
    }

    [TestMethod]
    public void When_ValidUserCredentials_Expect_UserIsNotNull()
    {
        var mockAuthenticationService …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing moq

1
推荐指数
1
解决办法
8766
查看次数

标签 统计

c# ×1

moq ×1

unit-testing ×1