如何模拟Save方法的行为来测试我的存储库?

Aleksej_Shherbak

我有以下存储库:

public class WorkspaceRepo : IWorkspacesRepo
{
    private readonly ApplicationContext _applicationContext;

    public WorkspaceRepo(ApplicationContext applicationContext)
    {
        _applicationContext = applicationContext;
    }

    public IEnumerable<Workspace> Workspaces => _applicationContext.Workspaces;

    public void Save(Workspace workspace)
    {
        _applicationContext.Workspaces.Add(workspace);
        _applicationContext.SaveChanges();
    }
}

以下方法来自我的业务逻辑类。我需要测试这种方法:

public Workspace CreateWorkspace(Workspace workspace)
{
    if (string.IsNullOrEmpty(workspace.UserUuid))
    {
        throw new RpcException(new Status(StatusCode.NotFound, "Empty user Uuid"));
    }

    var freSlots =  10 - _workspacesRepo.Workspaces.Count(x => x.UserUuid == workspace.UserUuid);

    if (freSlots <= 0)
    {
        throw new RpcException(new Status(StatusCode.Aborted, "There are no free slots work workspaces"));
    }

    _workspacesRepo.Save(workspace);

    return workspace;
}

业务逻辑很简单。我只能保存10个Workspace对象。下一次的储蓄一定要给我RpcException现在我要测试一下。这是测试代码:

[Test]
public void User_Cant_Exceed_Workspaces_Limit()
{
    // organization
    Mock<IWorkspacesRepo> mock = new Mock<IWorkspacesRepo>();
    mock.Setup(m => m.Save(It.IsAny<Workspace>())).Callback( /* what to do here?*/ )    

    var target = new WorkspaceBusinessService(mock.Object, null);

    for (var i = 0; i < 10; i++)
    {
        target.CreateWorkspace(new Workspace
        {
            Name = Guid.NewGuid().ToString(),
            UserUuid = Guid.NewGuid().ToString(),
            WorkspaceId = i + 1
        });
    }

    var redundantWorkspace = new Workspace
    {
        Name = Guid.NewGuid().ToString(),
        UserUuid = Guid.NewGuid().ToString(),
        WorkspaceId = 11
    };

    // action
    // asserts
    var ex = Assert.Throws<RpcException>(() => target.CreateWorkspace(redundantWorkspace));
    Assert.That(ex.Message, Is.EqualTo("Status(StatusCode.Aborted, \"There are no free slots work workspaces\")"));
}

但是预期的行为并没有发生。我用debug观看了这个,在CreateWorkspace方法中我总是10 freeSlots如何测试这种情况?

恩科西

根据被测方法中的逻辑,您正在模拟错误的成员。

模拟Workspaces属性,使其在预期时表现出预期

//..

var freSlots = 10 - _workspacesRepo.Workspaces.Count(x => x.UserUuid == workspace.UserUuid);

if (freSlots <= 0) {

//...

被调用。

例如

// Arrange
//need common user id
var userUuid = Guid.NewGuid().ToString();

//create workspaces to satisfy Linq Count(Expression)
var workspaces = Enumerable.Range(0, 10).Select(i => new Workspace {
    Name = Guid.NewGuid().ToString(),
    UserUuid = userUuid, //<-- Note the common user id
    WorkspaceId = i + 1
});

Mock<IWorkspacesRepo> mock = new Mock<IWorkspacesRepo>();

//set up the property to return the list
mock.Setup(_ => _.Workspaces).Returns(workspaces);

var target = new WorkspaceBusinessService(mock.Object, null);

var redundantWorkspace = new Workspace {
    Name = Guid.NewGuid().ToString(),
    UserUuid = userUuid, //<-- Note the common user id
    WorkspaceId = 11
};

// Act
Action act = () => target.CreateWorkspace(redundantWorkspace);

//Assert
var ex = Assert.Throws<RpcException>(act);
Assert.That(ex.Message, Is.EqualTo("Status(StatusCode.Aborted, \"There are no free slots work workspaces\")"));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Service for Unit测试中存储库的模拟方法如何测试

模拟存储库方法验证测试失败

如何使用模拟存储库测试CRUD?

如何构建数据库来存储用户对我网站的行为(关注,喜欢...)

为EF dbcontext设置模拟对象以测试存储库方法

单元测试学说:不能模拟存储库方法

Spock-模拟的存储库方法save()给出NullPointerException

单元测试如何使用Mockito模拟存储库

如何在Visual Studio测试中模拟数据存储库?

我如何使用反应测试库来测试 promise 函数?

我如何测试具有本地依赖关系的TypeORM存储库方法

模拟通用存储库单元测试

如何在Java中模拟MongoDB存储库方法

单元测试(模拟)数据库,如何通过模拟验证数据库方法?

我们如何模拟我们的服务来测试控制器?

如何模拟数据库的行为

Spring Boot Controller测试-模拟存储库但测试实际服务

使用Spring Boot中的模拟存储库测试void服务方法?

如何从标记为@Modifying的存储库中测试方法?

在对删除服务进行单元测试时,如何模拟void方法的行为?

我如何使用HashMap模拟我的Mongo数据库存储库

在静态方法中使用 Spring 存储库来设置测试数据

模拟通用存储库工厂方法

模拟表达参数到存储库方法

如何添加到模拟存储库

如何模拟存储库以用作Controller的参数?

如何访问从模拟库返回的模拟方法

使用 mockmvc 测试时无法模拟存储库

在Spock集成测试中模拟JPA存储库