public void DbSetCreateUserIfNotExist(string firstName, string lastName, DateTime dateOfBirth, string expectedError) { //arrange var users = new List <UserEntity> { new UserEntity { LastName = "ExistLastName", DateOfBirth = DateTime.Parse("01/20/2012", UsCultureInfo.DateTimeFormat) }, new UserEntity { FirstName = "ExistFirstName" }, new UserEntity { DateOfBirth = DateTime.Parse("01/20/2012", UsCultureInfo.DateTimeFormat) }, new UserEntity { DateOfBirth = DateTime.Parse("01/20/2012", UsCultureInfo.DateTimeFormat) }, new UserEntity { DateOfBirth = DateTime.Parse("01/20/2012", UsCultureInfo.DateTimeFormat) }, }; var mock = users.AsQueryable().BuildMockDbSet(); var userRepository = new TestDbSetRepository(mock); var service = new MyService(userRepository); //act var ex = Assert.ThrowsAsync <ApplicationException>(() => service.CreateUserIfNotExist(firstName, lastName, dateOfBirth)); //assert Assert.AreEqual(expectedError, ex.Message); }
public async Task DbSetGetUserReports(DateTime from, DateTime to, int expectedCount) { //arrange var users = new List <UserEntity> { new UserEntity { FirstName = "FirstName1", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012", UsCultureInfo.DateTimeFormat) }, new UserEntity { FirstName = "FirstName2", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012", UsCultureInfo.DateTimeFormat) }, new UserEntity { FirstName = "FirstName3", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012", UsCultureInfo.DateTimeFormat) }, new UserEntity { FirstName = "FirstName3", LastName = "LastName", DateOfBirth = DateTime.Parse("03/20/2012", UsCultureInfo.DateTimeFormat) }, new UserEntity { FirstName = "FirstName5", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2018", UsCultureInfo.DateTimeFormat) }, }; var mock = users.AsQueryable().BuildMockDbSet(); var userRepository = new TestDbSetRepository(mock); var service = new MyService(userRepository); //act var result = await service.GetUserReports(from, to); //assert Assert.AreEqual(expectedCount, result.Count); }
public void DbSetGetAllUserEntity() { //arrange var users = new List <UserEntity> { new UserEntity { FirstName = "FirstName1", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012", UsCultureInfo.DateTimeFormat) }, new UserEntity { FirstName = "FirstName2", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012", UsCultureInfo.DateTimeFormat) }, new UserEntity { FirstName = "FirstName3", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012", UsCultureInfo.DateTimeFormat) }, new UserEntity { FirstName = "FirstName3", LastName = "LastName", DateOfBirth = DateTime.Parse("03/20/2012", UsCultureInfo.DateTimeFormat) }, new UserEntity { FirstName = "FirstName5", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2018", UsCultureInfo.DateTimeFormat) }, }; var mock = users.AsQueryable().BuildMockDbSet(); var userRepository = new TestDbSetRepository(mock.Object); //act var result = userRepository.GetAll(); //assert Assert.AreEqual(users.Count, result.Count); }
public async Task DbSetCreateUser(string firstName, string lastName, DateTime dateOfBirth) { //arrange var mock = new List <UserEntity>().AsQueryable().BuildMockDbSet(); var userRepository = new TestDbSetRepository(mock); var service = new MyService(userRepository); //act await service.CreateUserIfNotExist(firstName, lastName, dateOfBirth); }
public async Task DbSetFindAsyncUserEntity() { //arrange var userId = Guid.NewGuid(); var users = new List <UserEntity> { new UserEntity { Id = Guid.NewGuid(), FirstName = "FirstName1", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012", UsCultureInfo.DateTimeFormat) }, new UserEntity { Id = Guid.NewGuid(), FirstName = "FirstName2", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012", UsCultureInfo.DateTimeFormat) }, new UserEntity { Id = userId, FirstName = "FirstName3", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012", UsCultureInfo.DateTimeFormat) }, new UserEntity { Id = Guid.NewGuid(), FirstName = "FirstName3", LastName = "LastName", DateOfBirth = DateTime.Parse("03/20/2012", UsCultureInfo.DateTimeFormat) }, new UserEntity { Id = Guid.NewGuid(), FirstName = "FirstName5", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2018", UsCultureInfo.DateTimeFormat) } }; var mock = users.AsQueryable().BuildMockDbSet(); mock.Setup(x => x.FindAsync(It.IsAny <object[]>())).ReturnsAsync((object[] ids) => { var id = (Guid)ids.First(); return(users.FirstOrDefault(x => x.Id == id)); }); var userRepository = new TestDbSetRepository(mock.Object); //act var result = await((DbSet <UserEntity>)userRepository.GetQueryable()).FindAsync(userId); //assert Assert.IsNotNull(result); Assert.AreEqual("FirstName3", result.FirstName); }
public async Task DbSetCreateUser(string firstName, string lastName, DateTime dateOfBirth) { //arrange var userEntities = new List <UserEntity>(); var mock = userEntities.AsQueryable().BuildMockDbSet(); mock.Setup(set => set.AddAsync(It.IsAny <UserEntity>(), It.IsAny <CancellationToken>())) .Callback((UserEntity entity, CancellationToken _) => userEntities.Add(entity)); var userRepository = new TestDbSetRepository(mock.Object); var service = new MyService(userRepository); //act await service.CreateUserIfNotExist(firstName, lastName, dateOfBirth); // assert var entity = mock.Object.Single(); Assert.AreEqual(firstName, entity.FirstName); Assert.AreEqual(lastName, entity.LastName); Assert.AreEqual(dateOfBirth, entity.DateOfBirth); }
public async Task DbSetCreateUser(string firstName, string lastName, DateTime dateOfBirth) { //arrange var userEntities = new List <UserEntity>(); var mock = userEntities.AsQueryable().BuildMockDbSet(); mock.AddAsync(Arg.Any <UserEntity>()) .Returns(info => null) .AndDoes(info => userEntities.Add(info.Arg <UserEntity>())); var userRepository = new TestDbSetRepository(mock); var service = new MyService(userRepository); //act await service.CreateUserIfNotExist(firstName, lastName, dateOfBirth); // assert var entity = mock.Single(); Assert.AreEqual(firstName, entity.FirstName); Assert.AreEqual(lastName, entity.LastName); Assert.AreEqual(dateOfBirth, entity.DateOfBirth); }