public void GetUserWorkspaces_WorkspacesExist_CachesResult() { string authUserId = Guid.NewGuid().ToString(); string cacheKey = CacheKeys.UserWorkspace(authUserId); IDbContext dbContext = Substitute.For <IDbContext>(); IEnumerable <WorkspaceViewModel>?cachedViewModel = null; UserDbModel userDbModel = new UserDbModel(); userDbModel.Workspaces = new string[1] { "workspace1" }; WorkspaceDbModel[] workspaceDbModels = new WorkspaceDbModel[1] { new WorkspaceDbModel() }; UserRepository userRepository = Substitute.For <UserRepository>(); userRepository.GetByAuthIdAsync(dbContext, authUserId).Returns <UserDbModel>(userDbModel); WorkspaceRepository workspaceRepository = Substitute.For <WorkspaceRepository>(); workspaceRepository.GetManyByIdAsync(dbContext, userDbModel.Workspaces).Returns(workspaceDbModels); IMemoryCacheWrapper memoryCache = Substitute.For <IMemoryCacheWrapper>(); memoryCache.Get <IEnumerable <WorkspaceViewModel> >(cacheKey).Returns(cachedViewModel); UserWorkspaceViewService userWorkspaceViewService = new UserWorkspaceViewService(dbContext, memoryCache, Substitute.For <IMapper>(), userRepository, workspaceRepository); IEnumerable <WorkspaceViewModel> result = userWorkspaceViewService.GetUserWorkspaces(authUserId).Result; // assert memoryCache.Received(1).Set <IEnumerable <WorkspaceViewModel> >(cacheKey, Arg.Any <IEnumerable <WorkspaceViewModel> >(), Arg.Any <TimeSpan>()); }
public void GetUserWorkspaces_UserWorkspacesNull_ReturnsEmptyEnumerable() { string authUserId = Guid.NewGuid().ToString(); string cacheKey = CacheKeys.UserWorkspace(authUserId); IDbContext dbContext = Substitute.For <IDbContext>(); IEnumerable <WorkspaceViewModel>?cachedViewModel = null; UserDbModel userDbModel = new UserDbModel(); UserRepository userRepository = Substitute.For <UserRepository>(); userRepository.GetByAuthIdAsync(dbContext, authUserId).Returns <UserDbModel>(userDbModel); IMemoryCacheWrapper memoryCache = Substitute.For <IMemoryCacheWrapper>(); memoryCache.Get <IEnumerable <WorkspaceViewModel> >(cacheKey).Returns(cachedViewModel); UserWorkspaceViewService userWorkspaceViewService = new UserWorkspaceViewService(dbContext, memoryCache, Substitute.For <IMapper>(), userRepository, Substitute.For <WorkspaceRepository>()); IEnumerable <WorkspaceViewModel> result = userWorkspaceViewService.GetUserWorkspaces(authUserId).Result; // assert Assert.AreEqual(0, result.Count()); memoryCache.Received(1).Set <IEnumerable <WorkspaceViewModel> >(cacheKey, Arg.Any <IEnumerable <WorkspaceViewModel> >(), Arg.Any <TimeSpan>()); }