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



我有以下存储库:

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")"));
}

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

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

模拟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")"));

最新更新