public void EnsureUserExistsAsync_UserExists_ReturnedWithoutDbCall() { IDbContext dbContext = Substitute.For <IDbContext>(); UserInfo userInfo = new UserInfo(); userInfo.AuthId = Guid.NewGuid().ToString(); userInfo.Name = Guid.NewGuid().ToString(); userInfo.Email = Guid.NewGuid().ToString(); UserDbModel userDbModel = new UserDbModel(); userDbModel.Id = Guid.NewGuid().ToString(); userDbModel.AuthId = userInfo.AuthId; UserRepository userRepository = Substitute.For <UserRepository>(); userRepository.GetByAuthIdAsync(dbContext, userInfo.AuthId).Returns(userDbModel); IMapper mapper = CreateMapper(); CreateUserCommand createUserCommand = Substitute.For <CreateUserCommand>(); UserViewService userViewService = new UserViewService(dbContext, mapper, userRepository, createUserCommand); UserViewModel result = userViewService.EnsureUserExistsAsync(userInfo).Result; Assert.AreEqual(userInfo.AuthId, result.AuthId); createUserCommand.DidNotReceive().ExecuteAsync(Arg.Any <IDbContext>(), Arg.Any <string>(), Arg.Any <string>(), Arg.Any <string>()).Wait(); }
public void EnsureUserExistsAsync_UserDoesNotExist_ReturnedAfterCreation() { IDbContext dbContext = Substitute.For <IDbContext>(); UserInfo userInfo = new UserInfo(); userInfo.AuthId = Guid.NewGuid().ToString(); userInfo.Name = Guid.NewGuid().ToString(); userInfo.Email = Guid.NewGuid().ToString(); UserDbModel userDbModel = new UserDbModel(); userDbModel.Id = Guid.NewGuid().ToString(); userDbModel.AuthId = userInfo.AuthId; UserRepository userRepository = Substitute.For <UserRepository>(); userRepository.GetByAuthIdAsync(dbContext, userInfo.AuthId).Returns <UserDbModel>(x => null); IMapper mapper = CreateMapper(); CreateUserCommand createUserCommand = Substitute.For <CreateUserCommand>(); createUserCommand.ExecuteAsync(dbContext, userInfo.AuthId, userInfo.Name, userInfo.Email).Returns(userDbModel); UserViewService userViewService = new UserViewService(dbContext, mapper, userRepository, createUserCommand); UserViewModel result = userViewService.EnsureUserExistsAsync(userInfo).Result; Assert.AreEqual(userInfo.AuthId, result.AuthId); createUserCommand.Received(1).ExecuteAsync(dbContext, userInfo.AuthId, userInfo.Name, userInfo.Email).Wait(); }