Advertisement
TeMePyT

Untitled

Aug 4th, 2019
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.91 KB | None | 0 0
  1. using LoWaiLo.Data;
  2. using LoWaiLo.Data.Common;
  3. using LoWaiLo.Data.Models;
  4. using LoWaiLo.Services.Contracts;
  5. using Microsoft.AspNetCore.Identity;
  6. using Microsoft.EntityFrameworkCore;
  7. using Microsoft.Extensions.Logging;
  8. using Microsoft.Extensions.Options;
  9. using Moq;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using Xunit;
  17.  
  18. namespace LoWaiLo.Services.Tests
  19. {
  20.     public class ShoppingCartServiceTests
  21.     {
  22.         private readonly IRepository<ShoppingCartProduct> shoppingCartProductsRepository;
  23.         private readonly LoWaiLoDbContext context;
  24.  
  25.         public ShoppingCartServiceTests()
  26.         {
  27.             var options = new DbContextOptionsBuilder<LoWaiLoDbContext>()
  28.                .UseInMemoryDatabase(Guid.NewGuid().ToString())
  29.                .Options;
  30.             this.context = new LoWaiLoDbContext(options);
  31.  
  32.             this.shoppingCartProductsRepository = new DbRepository<ShoppingCartProduct>(context);
  33.  
  34.         }
  35.  
  36.         [Fact]
  37.         public async Task AddProductInCartShouldAddProductInCart()
  38.         {
  39.             var userId = "123";
  40.             var userName = "user.abv.bg";
  41.             var user = new ApplicationUser { Id = userId, UserName = userName, ShoppingCart = new ShoppingCart() };
  42.             context.Users.Add(user);
  43.             context.SaveChanges();
  44.  
  45.             var userStore = new Mock<IUserStore<ApplicationUser>>();
  46.             userStore.Setup(s => s.FindByIdAsync(userId, CancellationToken.None)).ReturnsAsync(context.Users.First(x => x.Id == userId));
  47.             var userManager = TestUserManager(userStore.Object);
  48.  
  49.             var productId = 1;
  50.             var productsService = new Mock<IProductsService>();
  51.             productsService.Setup(p => p.GetProductById(productId))
  52.                 .Returns(new Product
  53.                 {
  54.                     Id = 123,
  55.                     Name = "Панирани зеленчуци",
  56.                     MenuNumber = 3,
  57.                     Price = 3.80m,
  58.                     Weight = 300,
  59.                     Image = "https://www.dropbox.com/s/r0r2ax7al6qtbjk/03.jpg?dl=0",
  60.                 });
  61.  
  62.             var shoppingCartService = new ShoppingCartService(shoppingCartProductsRepository, productsService.Object, userManager);
  63.  
  64.             await shoppingCartService.AddProductInCart(productId, userId);
  65.             var result1 = shoppingCartProductsRepository.All().ToList();
  66.  
  67.             Assert.Single(result1);
  68.             Assert.Equal(user.ShoppingCartId, result1.First().ShoppingCartId);
  69.             Assert.Equal(123, result1.First().ProductId);
  70.         }
  71.  
  72.         public static UserManager<TUser> TestUserManager<TUser>(IUserStore<TUser> store = null) where TUser : class
  73.         {
  74.             store = store ?? new Mock<IUserStore<TUser>>().Object;
  75.             var options = new Mock<IOptions<IdentityOptions>>();
  76.             var idOptions = new IdentityOptions();
  77.             idOptions.Lockout.AllowedForNewUsers = false;
  78.             options.Setup(o => o.Value).Returns(idOptions);
  79.             var userValidators = new List<IUserValidator<TUser>>();
  80.             var validator = new Mock<IUserValidator<TUser>>();
  81.             userValidators.Add(validator.Object);
  82.             var pwdValidators = new List<PasswordValidator<TUser>>();
  83.             pwdValidators.Add(new PasswordValidator<TUser>());
  84.             var userManager = new UserManager<TUser>(store, options.Object, new PasswordHasher<TUser>(),
  85.                 userValidators, pwdValidators, new UpperInvariantLookupNormalizer(),
  86.                 new IdentityErrorDescriber(), null,
  87.                 new Mock<ILogger<UserManager<TUser>>>().Object);
  88.             validator.Setup(v => v.ValidateAsync(userManager, It.IsAny<TUser>()))
  89.                 .Returns(Task.FromResult(IdentityResult.Success)).Verifiable();
  90.             return userManager;
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement