public void GetUserWorkspaces_NoUser_ReturnsEmptyEnumerable() { string authUserId = Guid.NewGuid().ToString(); string cacheKey = CacheKeys.UserWorkspace(authUserId); IDbContext dbContext = Substitute.For <IDbContext>(); IEnumerable <WorkspaceViewModel>?cachedViewModel = null; UserRepository userRepository = Substitute.For <UserRepository>(); userRepository.GetByAuthIdAsync(dbContext, authUserId).Returns <UserDbModel>(x => null); WorkspaceRepository workspaceRepository = Substitute.For <WorkspaceRepository>(); 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 Assert.AreEqual(0, result.Count()); userRepository.Received(1).GetByAuthIdAsync(dbContext, authUserId); workspaceRepository.DidNotReceive().GetManyByIdAsync(Arg.Any <IDbContext>(), Arg.Any <IEnumerable <string> >()); }
public void GetUserWorkspaces_WhenUserWorkspacesCached_DoesNotFetchFromRepository() { string authUserId = Guid.NewGuid().ToString(); string cacheKey = CacheKeys.UserWorkspace(authUserId); IDbContext dbContext = Substitute.For <IDbContext>(); IEnumerable <WorkspaceViewModel>?cachedViewModel = new WorkspaceViewModel[] { new WorkspaceViewModel() }; UserRepository userRepository = Substitute.For <UserRepository>(); WorkspaceRepository workspaceRepository = Substitute.For <WorkspaceRepository>(); 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.DidNotReceive().Set <IEnumerable <WorkspaceViewModel> >(Arg.Any <string>(), Arg.Any <IEnumerable <WorkspaceViewModel> >(), Arg.Any <TimeSpan>()); workspaceRepository.DidNotReceive().GetManyByIdAsync(Arg.Any <IDbContext>(), Arg.Any <IEnumerable <string> >()); userRepository.DidNotReceive().GetByAuthIdAsync(Arg.Any <IDbContext>(), Arg.Any <string>()); }