コード例 #1
0
        public void TestInitialize()
        {
            var serviceCollection = new ServiceCollection()
                                    .AddLogging();

            serviceCollection.Add(new ServiceDescriptor(typeof(DatabaseContext),
                                                        UnitTestDatabaseContext.Get()));
            serviceCollection.Add(new ServiceDescriptor(typeof(DaoManager),
                                                        DaoManager.Get(UnitTestDatabaseContext.Get())));
            var serviceProvider = serviceCollection.BuildServiceProvider();
            var factory         = serviceProvider.GetService <ILoggerFactory>();
            var logger          = factory.CreateLogger <OrderProductsController>();

            Controller = new OrderProductsController(logger)
            {
                ControllerContext = new ControllerContext
                {
                    HttpContext = new DefaultHttpContext
                    {
                        RequestServices = serviceProvider
                    }
                }
            };
            var identity = new ClaimsIdentity();

            identity.AddClaim(new Claim(ClaimTypes.Sid, "3"));
            identity.AddClaim(new Claim(ClaimTypes.Role, "Admin"));
            Controller.ControllerContext.HttpContext.User = new ClaimsPrincipal(identity);
        }
コード例 #2
0
        public void Initializer()
        {
            //New up everytime the test runs
            _mockContext            = new Mock <StoreContext>();
            _mockSetStoragePlace    = new Mock <DbSet <StoragePlace> >();
            _mockSetOrder           = new Mock <DbSet <Order> >();
            _mockSetCustomerAddress = new Mock <DbSet <CustomerAddress> >();
            _mockSetOrderProduct    = new Mock <DbSet <OrderProduct> >();
            _mockSetCustomer        = new Mock <DbSet <Customer> >();
            _mockSetAddress         = new Mock <DbSet <Address> >();
            _mockSetProduct         = new Mock <DbSet <Product> >();


            //Add data
            var dataStoragePlace    = ResourceData.StoragePlaces.AsQueryable();
            var dataOrder           = ResourceData.Orders.AsQueryable();
            var dataCustomerAddress = ResourceData.CustomerAddresses.AsQueryable();
            var dataOrderProduct    = ResourceData.OrderProducts.AsQueryable();
            var dataCustomer        = ResourceData.Customers.AsQueryable();
            var dataAddress         = ResourceData.Addresses.AsQueryable();
            var dataProduct         = ResourceData.Products.AsQueryable();

            //Setup behavior
            var setupDbSp = Helper.SetupDb(_mockSetStoragePlace, dataStoragePlace);
            var setupDbOr = Helper.SetupDb(_mockSetOrder, dataOrder);
            var setupDbCA = Helper.SetupDb(_mockSetCustomerAddress, dataCustomerAddress);
            var setupDbOP = Helper.SetupDb(_mockSetOrderProduct, dataOrderProduct);
            var setupDbCu = Helper.SetupDb(_mockSetCustomer, dataCustomer);
            var setupDbAd = Helper.SetupDb(_mockSetAddress, dataAddress);
            var setupDbPr = Helper.SetupDb(_mockSetProduct, dataProduct);


            _mockContext.Setup(x => x.StoragePlaces).Returns(setupDbSp.Object);
            _mockContext.Setup(x => x.Orders).Returns(setupDbOr.Object);
            _mockContext.Setup(x => x.CustomerAddresses).Returns(setupDbCA.Object);
            _mockContext.Setup(x => x.OrderProducts).Returns(setupDbOP.Object);
            _mockContext.Setup(x => x.Customers).Returns(setupDbCu.Object);
            _mockContext.Setup(x => x.Addresses).Returns(setupDbAd.Object);
            _mockContext.Setup(x => x.Products).Returns(setupDbPr.Object);


            //This will make the mock version of the db approve any string given to the include method.
            //Without this you will get null reference exception when calling include.
            _mockSetStoragePlace.Setup(x => x.Include(It.IsAny <string>())).Returns(_mockSetStoragePlace.Object);
            _mockSetOrder.Setup(x => x.Include(It.IsAny <string>())).Returns(_mockSetOrder.Object);
            _mockSetCustomerAddress.Setup(x => x.Include(It.IsAny <string>())).Returns(_mockSetCustomerAddress.Object);
            _mockSetOrderProduct.Setup(x => x.Include(It.IsAny <string>())).Returns(_mockSetOrderProduct.Object);
            _mockSetCustomer.Setup(x => x.Include(It.IsAny <string>())).Returns(_mockSetCustomer.Object);
            _mockSetAddress.Setup(x => x.Include(It.IsAny <string>())).Returns(_mockSetAddress.Object);
            _mockSetProduct.Setup(x => x.Include(It.IsAny <string>())).Returns(_mockSetProduct.Object);


            //Inject mock data via overload constructor
            var dbStoragePlaceRepository    = new DbStoragePlaceRepository(_mockContext.Object);
            var dbOrderRepository           = new DbOrderRepository(_mockContext.Object);
            var dbCustomerAddressRepository = new DbCustomerAddressRepository(_mockContext.Object);
            var dbOrderProductRepository    = new DbOrderProductRepository(_mockContext.Object);
            var dbAddressRepository         = new DbAddressRepository(_mockContext.Object);
            var dbStorageRepository         = new DbStoragePlaceRepository(_mockContext.Object);
            var dbCustomerRepository        = new DbCustomerRepository(_mockContext.Object);
            var dbProductRepository         = new DbProductRepository(_mockContext.Object);


            //Setup fakerepo via overloaded constructor
            _orderProductsController = new OrderProductsController(dbOrderProductRepository, dbOrderRepository, dbProductRepository);
        }