public async Task DoesGetAllCookingVesselsSelectListAsyncWorkCorrectly() { var list = new TestAsyncEnumerable <CookingVessel>(new List <CookingVessel> { new CookingVessel { Name = TestName, Form = PanForm.Circle, Diameter = TestDiameter, Height = TestHeight, Area = 314.33, Volume = 2200.33, }, }).AsQueryable(); var mockCookingVesselsRepo = new Mock <IDeletableEntityRepository <CookingVessel> >(MockBehavior.Strict); mockCookingVesselsRepo.Setup(x => x.AllAsNoTracking()).Returns(list); var mockRecipesRepo = new Mock <IDeletableEntityRepository <Recipe> >(); var service = new CookingVesselsService(mockCookingVesselsRepo.Object, mockRecipesRepo.Object); var cookingVessels = await service.GetAllCookingVesselsSelectListAsync(); var count = cookingVessels.ToList().Count(); Assert.Equal(1, count); }
public async Task DoesGetAllCategoriesForRecipeAsyncWorkCorrectly() { var list = new TestAsyncEnumerable <CategoryRecipe>(new List <CategoryRecipe> { new CategoryRecipe { Category = new Category { Id = 1, Name = "TestCategory1", }, RecipeId = TestRecipeId, }, new CategoryRecipe { Category = new Category { Id = 2, Name = "TestCategory2", }, RecipeId = TestRecipeId, }, }).AsQueryable(); var mockCategoryRecipesRepo = new Mock <IDeletableEntityRepository <CategoryRecipe> >(); mockCategoryRecipesRepo.Setup(x => x.AllAsNoTracking()).Returns(list); var service = new CategoryRecipesService(mockCategoryRecipesRepo.Object); var categoriesResult = await service.GetAllCategoriesForRecipeAsync(TestRecipeId); Assert.Equal(2, categoriesResult.Count); Assert.Equal(1, categoriesResult[0]); }
public static DbQuery <TEntity> BuildMockDbQuery <TEntity>(this IQueryable <TEntity> data) where TEntity : class { var mock = Substitute.For <DbQuery <TEntity>, IQueryable <TEntity>, IAsyncEnumerable <TEntity> >(); var enumerable = new TestAsyncEnumerable <TEntity>(data); mock.ConfigureAsyncEnumerableCalls(enumerable); mock.ConfigureQueryableCalls(enumerable, data); return(mock); }
public static Mock <DbSet <TEntity> > BuildMockDbSet <TEntity>(this IQueryable <TEntity> data) where TEntity : class { var mock = new Mock <DbSet <TEntity> >(); var enumerable = new TestAsyncEnumerable <TEntity>(data); mock.As <IAsyncEnumerable <TEntity> >().ConfigureAsyncEnumerableCalls(enumerable); mock.As <IQueryable <TEntity> >().ConfigureQueryableCalls(enumerable, data); return(mock); }
public static DbQuery <TEntity> BuildMockDbQuery <TEntity>(this IQueryable <TEntity> data) where TEntity : class { var mock = A.Fake <DbQuery <TEntity> >( d => d.Implements <IAsyncEnumerable <TEntity> >().Implements <IQueryable <TEntity> >()); var enumerable = new TestAsyncEnumerable <TEntity>(data); mock.ConfigureQueryableCalls(enumerable, data); mock.ConfigureAsyncEnumerableCalls(enumerable); return(mock); }
public static Mock <IQueryable <TEntity> > BuildMock <TEntity>(this IQueryable <TEntity> data) where TEntity : class { var mock = new Mock <IQueryable <TEntity> >(); var enumerable = new TestAsyncEnumerable <TEntity>(data); mock.As <IAsyncEnumerable <TEntity> >().Setup(d => d.GetEnumerator()).Returns(enumerable.GetEnumerator); mock.As <IQueryable <TEntity> >().Setup(m => m.Provider).Returns(enumerable); mock.As <IQueryable <TEntity> >().Setup(m => m.Expression).Returns(data?.Expression); mock.As <IQueryable <TEntity> >().Setup(m => m.ElementType).Returns(data?.ElementType); mock.As <IQueryable <TEntity> >().Setup(m => m.GetEnumerator()).Returns(data?.GetEnumerator()); return(mock); }
public static IQueryable <TEntity> BuildMock <TEntity>(this IQueryable <TEntity> data) where TEntity : class { var mock = A.Fake <IQueryable <TEntity> >(d => d.Implements <IAsyncEnumerable <TEntity> >().Implements <IQueryable <TEntity> >()); var enumerable = new TestAsyncEnumerable <TEntity>(data); A.CallTo(() => ((IAsyncEnumerable <TEntity>)mock).GetEnumerator()).Returns(enumerable.GetEnumerator()); A.CallTo(() => ((IQueryable <TEntity>)mock).Provider).Returns(enumerable); A.CallTo(() => ((IQueryable <TEntity>)mock).Expression).Returns(data?.Expression); A.CallTo(() => ((IQueryable <TEntity>)mock).ElementType).Returns(data?.ElementType); A.CallTo(() => ((IQueryable <TEntity>)mock).GetEnumerator()).Returns(data?.GetEnumerator()); return(mock); }
public void SetUp() { _mockLogger = new Mock <ILogger <IAdministratorService> >(); _mockRoleManager = MockFactory.MockRoleManager <IdentityRole>(); var roles = new List <IdentityRole>() { new IdentityRole(DefaultRoles.Administrator) { Id = DefaultRoles.Administrator.ToUpper() }, new IdentityRole(DefaultRoles.User) { Id = DefaultRoles.User.ToUpper() }, new IdentityRole("UpdateRole") { Id = "UpdateRole".ToUpper() } }; var asyncRolesEnumerable = new TestAsyncEnumerable <IdentityRole>(roles); _mockRoleManager.Setup(_ => _.Roles).Returns(asyncRolesEnumerable.AsQueryable()).Verifiable(); _mockRoleManager.Setup(_ => _.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new IdentityRole(DefaultRoles.Administrator)).Verifiable(); _mockUserManager = MockFactory.MockUserManager <ApplicationUser>(); var users = new List <ApplicationUser>() { new ApplicationUser() { Id = "TestUser", Email = "*****@*****.**", FirstName = "Test", LastName = "User", OfficeLocation = new OfficeLocation() { Name = "Indy", Address = "Address", City = "Indianapolis", Country = "USA", State = "IN", Zip = "46240" }, UserName = "******", Title = "Tester" } }; var asyncUserEnumerable = new TestAsyncEnumerable <ApplicationUser>(users); _mockUserManager.Setup(_ => _.Users).Returns(asyncUserEnumerable.AsQueryable()).Verifiable(); _administratorService = new AdministratorService(_mockLogger.Object, _mockRoleManager.Object, _mockUserManager.Object); }
public static IQueryable <TEntity> BuildMock <TEntity>(this IQueryable <TEntity> data) where TEntity : class { var mock = Substitute.For <IQueryable <TEntity>, IAsyncEnumerable <TEntity> > (); var enumerable = new TestAsyncEnumerable <TEntity>(data); ((IAsyncEnumerable <TEntity>)mock).GetEnumerator().Returns(enumerable.GetEnumerator()); mock.Provider.Returns(enumerable); mock.Expression.Returns(data?.Expression); mock.ElementType.Returns(data?.ElementType); mock.GetEnumerator().Returns(data?.GetEnumerator()); return(mock); }
public async Task GetRolesAsync_ReturnsNoRoles_WhenNoRoles() { // Arrange var roles = new List <IdentityRole>(); var asyncRolesEnumerable = new TestAsyncEnumerable <IdentityRole>(roles); _mockRoleManager.Setup(_ => _.Roles).Returns(asyncRolesEnumerable.AsQueryable()).Verifiable(); // Act var result = await _administratorService.GetRolesAsync(); // Assert Assert.That(result.Succeeded, Is.False); _mockRoleManager.Verify(_ => _.Roles, Times.Once); _mockRoleManager.Verify(_ => _.GetClaimsAsync(It.IsAny <IdentityRole>()), Times.Never); }
private static IQueryable <Customers> GetTestCustomers() { var customers = new TestAsyncEnumerable <Customers>(new List <Customers>() { new Customers { CustomerId = "ID1", ContactName = "Name1", ContactTitle = "Title1", Address = "Address1", City = "City1", Country = "Country1", CompanyName = "Company1", Fax = "Fax1", Phone = "Phone1", PostalCode = "Postal1", Region = "Region1", Orders = new List <Orders>() { new Orders(), new Orders() }, }, new Customers { CustomerId = "ID2", ContactName = "Name2", ContactTitle = "Title2", Address = "Address2", City = "City2", Country = "Country2", CompanyName = "Company2", Fax = "Fax2", Phone = "Phone2", PostalCode = "Postal2", Region = "Region2", Orders = new List <Orders>() { new Orders() }, } }); return(customers.AsQueryable()); }
public async Task GetUserOrders() { //Arrange var user = new User { Email = "*****@*****.**", Password = "******" }; var purchaseBooks = new TestAsyncEnumerable <PurchasedBook>(new[] { new PurchasedBook { PurchasedOn = DateTime.Now, PaymentMode = "Card", Book = new Book { AuthorName = "Gurwinder", WrittenIn = "English", Name = "Test", } }, new PurchasedBook { PurchasedOn = DateTime.Now, PaymentMode = "Card", Book = new Book { AuthorName = "Gurwinder", WrittenIn = "Hindi", Name = "Test", } } }).AsQueryable(); var sut = new UserOrderQueryHandler(_mockPurchasedBookRepository.Object, _mockUserRepository.Object, _mapper); _mockUserRepository.Setup(x => x.FindAsync(It.IsAny <Expression <Func <User, bool> > >(), It.IsAny <CancellationToken>())).ReturnsAsync(user); _mockPurchasedBookRepository.Setup(x => x.FindAll(It.IsAny <Expression <Func <PurchasedBook, bool> > >())).Returns(purchaseBooks); //Act var result = await sut.Handle(new UserOrderQuery { UserEmail = "*****@*****.**" }, default(CancellationToken)); //Assert result.Should().BeOfType <List <UserOrderModel> >(); result.Should().HaveCount(purchaseBooks.Count()); }
private static Task <HttpResponseMessage> GetTestHttpClientResponseMessage() { var customers = new TestAsyncEnumerable <CustomerBindingModel>(new List <CustomerBindingModel>() { new CustomerBindingModel { CustomerId = "ID1", ContactName = "Name1", ContactTitle = "Title1", Address = "Address1", City = "City1", Country = "Country1", CompanyName = "Company1", Fax = "Fax1", Phone = "Phone1", PostalCode = "Postal1", Region = "Region1", OrdersCount = 2, }, new CustomerBindingModel { CustomerId = "ID2", ContactName = "Name2", ContactTitle = "Title2", Address = "Address2", City = "City2", Country = "Country2", CompanyName = "Company2", Fax = "Fax2", Phone = "Phone2", PostalCode = "Postal2", Region = "Region2", OrdersCount = 0 } }); var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(customers)) }; return(Task.FromResult(response)); }
private void SetUpNotificationsRepository() { _notificationsRepository.Setup(r => r.Where(It.IsAny <Expression <Func <Notification, bool> > >())) .Returns((Expression <Func <Notification, bool> > predicate) => { var notifications = new TestAsyncEnumerable <Notification>(new[] { new Notification { Id = 1, Icon = "face", Message = "El cliente manolo se ha registrado.", State = NotificationState.Pending, Title = "Nuevo cliente", UserId = "333-555-333" }, new Notification { Id = 1, Icon = "payments", Message = "El cliente manolo ha realizado el pego de su factura.", State = NotificationState.Pending, Title = "Pago de factura", UserId = "333-555-332" } }); return(notifications.Where(predicate)); }); _notificationsRepository.Setup(r => r.FindByIdAsync(1)).ReturnsAsync(new Notification { Id = 1, Icon = "face", Message = "El cliente Manolo se ha registrado", Title = "Registro de cliente", State = NotificationState.Pending, UserId = "333-555-555" }); _notificationsRepository.Setup(r => r.FindByIdAsync(3)).ReturnsAsync((Notification)null); _notificationsRepository.Setup(r => r.Update(It.IsAny <Notification>())).Verifiable(); }
public async void IAsyncQueryable_HowItWorks() { IQueryable <TestEntity> tests = new TestAsyncEnumerable <TestEntity>(new List <TestEntity>() { new TestEntity() { Name = "satoshi" }, new TestEntity() { Name = "kaoru" } }.AsQueryable()); IList <TestEntity> result = await tests.Where(t => t.Name == "satoshi").ToListAsync(); _output.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented)); Assert.Equal("", JsonConvert.SerializeObject(result, Formatting.Indented)); }
public void ShouldReturnListAsTestAsyncEnumerable() { // Arrange var persons = new List <Person> { Testdata.Employees.CreateEmployee1(), Testdata.Employees.CreateEmployee2(), Testdata.Employees.CreateEmployee3() }; var queryablePersons = new TestAsyncEnumerable <Person>(persons); var personRepositoryMock = new Mock <IPersonRepository>(); personRepositoryMock.Setup(p => p.Get()) .Returns(queryablePersons); // Act var mockedPersons = personRepositoryMock.Object.Get(); // Assert mockedPersons.Should().NotBeNullOrEmpty(); mockedPersons.Should().HaveCount(persons.Count); }
public async Task GetBooksTest() { // Arrange var books = new TestAsyncEnumerable <Book>(new [] { new Book { Name = "Half Girlfriend", AuthorName = "Chetan Bhagat", Cost = 900, PublishedOn = DateTime.Now.AddYears(-6), WrittenIn = "English", Description = "Lorum Ipsum is dummy data" }, new Book { Name = "Five Point Someone", AuthorName = "Chetan Bhagat", Cost = 1200, PublishedOn = DateTime.Now.AddYears(-16), WrittenIn = "English", Description = "Lorum Ipsum is dummy data" } }).AsQueryable(); var sut = new BookListQueryHandler(_mockBookRepository.Object, _mapper); _mockBookRepository.Setup(x => x.GetAll()).Returns(books); // Act var result = await sut.Handle(new BookListQuery(), CancellationToken.None); // Assert result.Should().BeOfType <List <BookListViewModel> >(); result.Should().HaveCount(books.Count()); }