public async Task Get_AllProducts_CallsService_ReturnsOk()
        {
            var products = new List <Product>()
            {
                new Product()
                {
                    Name = "Virus"
                },
                new Product()
                {
                    Name = "Cold"
                }
            };

            _productService
            .Setup(call => call.GetProducts(It.IsAny <string>()))
            .Returns(Task.FromResult(products.AsEnumerable()))
            .Verifiable();


            var result = await _productsController.Get();

            var items = result.Items;

            Assert.That(items.Count, Is.EqualTo(2));
        }
        public void GetAllProducts_ReturnProductsById()
        {
            // Arrange
            var productservice = Substitute.For <IProductService>();
            int _id            = 13860428;
            var productlist    = Productslist().Where(a => a.Id == _id).FirstOrDefault();

            productservice.GetById(_id).Returns(productlist);
            productservice.GetProductNameandPrice(productlist, true).Returns(productlist);
            var productcontroller = new ProductsController(productservice)
            {
                Request       = new HttpRequestMessage(),
                Configuration = new HttpConfiguration()
            };

            // Act
            var     productsfromcontroller = productcontroller.Get(_id).Content.ReadAsStringAsync().Result;
            JObject jObject = JObject.Parse(productsfromcontroller);

            var products = productcontroller.Get(_id);

            Assert.IsTrue(products.TryGetContentValue <Products>(out Products product));

            // Assert
            Assert.AreEqual(productlist.ProductName, product.ProductName);
            Assert.AreEqual(_id, jObject["Id"]);
        }
        public void GetTest()
        {
            var result   = _productsController.Get();
            var okResult = Assert.IsType <OkObjectResult>(result);

            Assert.IsType <List <Product> >(okResult.Value);
        }
Esempio n. 4
0
        public void GetByIdSuccessTest()
        {
            bus.Setup(w => w.RequestAsync <string, ProductModel>(It.IsAny <string>())).Returns(Task.FromResult(new ProductModel()));
            var result = productsController.Get(123);

            Assert.IsInstanceOfType(result, typeof(Task <ActionResult>));
        }
        public void GetProduct_ReturnProductSameID()
        {
            //arrange
            // these two lines are the moq equivalent of this:
            // var context = new TestStoreAppContext();
            // context.Products.Add(GetDemoProduct());

            var mockContext = new Mock <IBookStoreAppContext>();

            mockContext.Setup(x => x.Get(3)).Returns(GetDemoProduct());

            // act
            // sut (system under test)
            var sut = new ProductsController(mockContext.Object);

            // assert

            // try to get an object that doesn't exist and we should get a not found
            var result = sut.Get(1);

            Assert.IsInstanceOfType(result, typeof(NotFoundObjectResult));

            var httpResult = sut.Get(3) as OkObjectResult;
            var product    = httpResult.Value as Product;

            // trying to get a product that exists should return our demo object
            // this could have been done in a better way than comparing field by field...but time
            Assert.AreEqual(product.Id, GetDemoProduct().Id);
            Assert.AreEqual(product.Name, GetDemoProduct().Name);
            Assert.AreEqual(product.Price, GetDemoProduct().Price);
        }
Esempio n. 6
0
        public void UpdatesProductCorrectly()
        {
            using (var context = new ProductContext(optionsBuilder.Options))
            {
                context.Database.EnsureCreated();

                ProductsController productsController = new ProductsController(context, mapper);

                Guid       id                = new Guid("2136EAAD-4D96-435C-ABCC-7A3B549CFCDD");
                var        productInitial    = productsController.Get(id);
                ProductDto productDtoInitial = (productInitial.Result as OkObjectResult).Value as ProductDto;

                Assert.That("Asus", Is.Not.EqualTo(productDtoInitial.Description));

                var statusUpdated = productsController.UpdateDescription(id, "Asus");

                Assert.That(statusUpdated, Is.InstanceOf(typeof(NoContentResult)));
                Assert.That((statusUpdated as NoContentResult).StatusCode, Is.EqualTo(204));

                var        productUpdated    = productsController.Get(id);
                ProductDto productDtoUpdated = (productUpdated.Result as OkObjectResult).Value as ProductDto;

                Assert.That("Asus", Is.EqualTo(productDtoUpdated.Description));
            }
        }
Esempio n. 7
0
 public void Get_Returns_All()
 {
     // Act
     var results = _controller.Get() as ActionResult <IEnumerable <Product> >;
     // Assert
     var items = Assert.IsType <ActionResult <IEnumerable <Product> > >(results);
     //Assert.Equal(3, results.Value.Count());
 }
Esempio n. 8
0
        public void GetAllProducts()
        {
            TestHelper.InitDatabase(); GetReady();
            var actRes   = controller.Get();
            var response = actRes.ExecuteAsync(CancellationToken.None).Result;

            Assert.IsNotNull(response.Content);
        }
        public void GetProductById()
        {
            GetReady();
            var actRes   = controller.Get(1);
            var response = actRes.ExecuteAsync(CancellationToken.None).Result;

            Assert.IsNotNull(response.Content);
        }
Esempio n. 10
0
        public void GetById_ExistingIdPassed_ReturnsOkResult()
        {
            // Act
            var okResult = _controller.Get(1);

            // Assert
            Assert.IsType <OkObjectResult>(okResult);
        }
Esempio n. 11
0
        public async Task GetAllProducts_ShouldReturnTwoItems()
        {
            //Act
            var result = await _controller.Get();

            //Assert
            result.Should().BeOfType <List <ProductDto> >();
            Assert.AreEqual(2, result.Count);
        }
        public async Task ItGetsPagedProducts()
        {
            var mock = new PagedResultDto <ProductDto>(Mock.Of <List <ProductDto> >(), Mock.Of <PagerDto>());

            service.Setup(s => s.GetAsync(It.IsAny <PagerDto>(), It.IsAny <short>())).ReturnsAsync(mock);
            var result = await ctrl.Get((short)1);

            Assert.IsType <PagedResultDto <ProductDto> >(result);
        }
Esempio n. 13
0
        public void Get_gets_all_Products_via_service()
        {
            //Act
            var products = _Controller.Get();

            //Assert
            Assert.IsNotNull(products);
            Assert.AreEqual(products.Items.Count, 2);
        }
Esempio n. 14
0
        public async Task GetProducts_NoUserId_RequestsAllProducts()
        {
            //Arrange

            //Act
            ActionResult <List <Product> > response = await _classUnderTest.Get(0);

            //Assert
            _productsProviderMock.Verify(x => x.GetFoodProductsByCarbs());
        }
Esempio n. 15
0
        public void GetAll_ReturnsSuccess()
        {
            //act
            var result = ProductsController.Get() as OkNegotiatedContentResult <IQueryable <Product> >;

            //assert
            A.CallTo(() => ProductRepository.Retrieve()).MustHaveHappened();
            Assert.NotNull(result);
            Assert.Equal(result.Content, MockedProducts);
        }
Esempio n. 16
0
        public async Task given_invalid_id_product_should_not_be_returned()
        {
            var productId = Guid.NewGuid();
            var result    = await _controller.Get(productId);

            await _dispatcher.Received().QueryAsync(Arg.Is <GetProduct>(
                                                        q => q.Id == productId));

            result.Value.ShouldBeNull();
        }
        public void GetActionTest()
        {
            var controller = new ProductsController();

            Assert.IsType <OkObjectResult>(controller.Get());
            foreach (var key in FakeData.Products.Keys)
            {
                Assert.IsType <OkObjectResult>(controller.Get(key));
            }
        }
        public void Should_Call_GetProducts_On_ProductService()
        {
            var actionResult = _productsController.Get();
            var result       = actionResult as OkObjectResult;

            result.Should().NotBeNull();
            result.Value.Should().NotBeNull();

            _mockedService.Verify(r => r.GetProducts(), Times.Once);
        }
Esempio n. 19
0
        public async void Get_ShouldReturnProduct()
        {
            _mockService.Setup(s => s.GetAsync(It.IsAny <string>())).Returns(Task.FromResult(product1));
            var result = await _productcController.Get("1");

            var objectResult = result.Result as OkObjectResult;

            Assert.NotNull(objectResult);
            Assert.Equal(200, objectResult.StatusCode);
        }
Esempio n. 20
0
        public void GetAllProductsReturnsCorrectListFromRepository()
        {
            // Arrange
            repository.Setup(x => x.GetAll()).Returns(products);

            // Act
            var result = sut.Get();

            // Assert
            result.ShouldBe(products);
        }
        public void Test_Get_Product_by_Id_ReturnsOkResult()
        {
            var response = _productController.Get(1);

            Assert.IsType <OkObjectResult>(response.Result);

            var okResult = response.Result as OkObjectResult;
            var product  = Assert.IsType <ProductDto>(okResult.Value);

            Assert.Equal("Samsung Galaxy S20", product.Name);
        }
        public async void GetProductsByIdTest()
        {
            //Given

            //When
            var result = await _controller.Get(1);

            //Then
            Assert.NotNull(result);          // Test if null
            Assert.IsType <Product>(result); // Test type
            Assert.Equal(1, result.Id);      // Verify it is the right product
        }
Esempio n. 23
0
        public void product_delete_returnTrue()
        {
            // Arrange
            ProductsController controller = new ProductsController(new ProductService(new ProductRepository()));
            var expects = controller.Get();

            // Act
            bool actual      = controller.Delete(expects.ToList().LastOrDefault().ProductID);
            int  actualCount = controller.Get().Count();

            // Assert
            Assert.IsTrue(actual);
            Assert.AreEqual(expects.Count(), actualCount + 1);
        }
Esempio n. 24
0
        public async Task GetAsync_ProductById()
        {
            // Arrange
            var guid = new Guid();
            //var recordNotFoundId = new Guid();
            //var exceptionId = new Guid();

            var mockIProductService = new Mock <IProductService>();

            mockIProductService
            .Setup(arg => arg.GetProductAsync(guid))
            .ReturnsAsync(new Product());

            var mockIProductOptionService = new Mock <IProductOptionService>();

            var sut = new ProductsController(mockIProductService.Object, mockIProductOptionService.Object);

            // Act
            var response = await sut.Get(guid);

            // Assert
            Assert.IsType <OkObjectResult>(response);
            var objectResponse = response as ObjectResult;

            Assert.Equal(200, objectResponse.StatusCode);

            // Arrange
            mockIProductService.Setup(arg => arg.GetProductAsync(guid))
            .ThrowsAsync(new RecordNotFoundException(guid, "product"));

            // Act
            response = await sut.Get(guid);

            // Assert
            Assert.IsType <NotFoundObjectResult>(response);
            objectResponse = response as ObjectResult;
            Assert.Equal(404, objectResponse.StatusCode);
            Assert.Equal($"product not found for the Id {guid}", objectResponse.Value);

            // Arrange
            mockIProductService.Setup(arg => arg.GetProductAsync(guid))
            .ThrowsAsync(new Exception());

            // Act
            response = await sut.Get(guid);

            // Assert
            Assert.IsType <StatusCodeResult>(response);
            Assert.Equal(500, ((StatusCodeResult)response).StatusCode);
        }
Esempio n. 25
0
        public void GetAll_Returns_Ok_When_All_Valid()
        {
            //Arrange

            var productList = new List <Product> {
                new Product {
                    ProductID          = 1,
                    ProductName        = "milk",
                    ProductDescription = "2%Milk",
                    UnitsInStock       = 250,
                    SellPrice          = 3.99m,
                    DiscountPercentage = 10,
                    UnitsMax           = 1250
                },
                new Product {
                    ProductID          = 2,
                    ProductName        = "bread",
                    ProductDescription = "White",
                    UnitsInStock       = 1050,
                    SellPrice          = 4.99m,
                    DiscountPercentage = 5,
                    UnitsMax           = 6250
                },
                new Product {
                    ProductID          = 3,
                    ProductName        = "butter",
                    ProductDescription = "unsalted",
                    UnitsInStock       = 50,
                    SellPrice          = 1.99m,
                    DiscountPercentage = 7,
                    UnitsMax           = 500
                }
            };

            mockService.Setup(m => m.GetProductList())
            .Returns(productList);

            //Act

            ActionResult actionResult = controller.Get();

            //Asserts

            Assert.NotNull(actionResult);
            var result = Assert.IsType <OkObjectResult>(actionResult);

            List <Product> list = result.Value as List <Product>;

            Assert.Equal(3, list.Count);
        }
        public async Task GetProducts_ReturnsOkResult_IfProductsFound()
        {
            //Arrange
            var products = _fixture.CreateMany <Product>(3);

            _productService.Setup(x => x.GetProductByNameAsync(null)).ReturnsAsync(products);

            //Act
            var result = await _productsController.Get();

            //Assert
            result.Should().NotBeNull().And.BeAssignableTo <IStatusCodeActionResult>()
            .Which.StatusCode.Should().Be((int)HttpStatusCode.OK);
        }
Esempio n. 27
0
        public void GetProductByID()
        {
            var target = new ProductsController(this._EFProductRepository, this._MockMapper);

            var okResult = target.Get(3) as OkObjectResult;
            var product  = (ReturnProductDto)okResult.Value;

            Assert.Equal(200, okResult.StatusCode);
            Assert.Equal("TestProduct01", product.Name.ToString());

            var badRequestResult = target.Get(4) as BadRequestObjectResult;

            Assert.Equal(400, badRequestResult.StatusCode);
        }
Esempio n. 28
0
        public async Task WhenIProvideUserId_ItFiltersTheResults()
        {
            var productsController = new ProductsController(new LdClient("key"), new ProductsProvider(_connectionString), new InMemoryUsersProvider());

            ActionResult <List <Product> > response0 = await productsController.Get(0);

            ActionResult <List <Product> > response1 = await productsController.Get(1);

            var result0   = response0.Result as ObjectResult;
            var products0 = result0?.Value as List <Product>;
            var result1   = response1.Result as ObjectResult;
            var products1 = result1?.Value as List <Product>;

            Assert.IsTrue(products0.Count > products1.Count);
        }
        public void CanGetAllProducts()
        {
            var controller = new ProductsController(db);
            var result     = controller.Get();

            Assert.NotNull(result);
        }
Esempio n. 30
0
        public void get_product_detail_by_id_return_product_detail()
        {
            // Arrange
            int product_id = 2;

            var mockService = new Mock <IProductService>();

            mockService.Setup(
                service => service.getProductDetail(product_id))
            .Returns(
                new ProductsModel {
                id = 2, name = "43 Piece dinner Set", gender = "Female", age = "3_to_5", availability = "InStock", brand = "CoolKidz"
            }
                );

            var controller = new ProductsController(mockService.Object);

            var expected_product = new ProductsModel {
                id = 2, name = "43 Piece dinner Set", gender = "Female", age = "3_to_5", availability = "InStock", brand = "CoolKidz"
            };

            //Act
            var result = controller.Get(product_id);

            // Assert
            var user = Assert.IsType <ProductsModel>(result);

            Assert.Equal("43 Piece dinner Set", user.name);
        }
        public void Get_ShouldReturnProductWithSameId()
        {
            // Arrange
            var mockRepository = new Mock<IProductRepository>();
            mockRepository.Setup(x => x.GetProduct("Found"))
                .Returns(new Product { Id = "Found" });

            var controller = new ProductsController(mockRepository.Object);

            // Act
            IHttpActionResult actionResult = controller.Get("Found");
            var contentResult = actionResult as OkNegotiatedContentResult<Product>;

            // Assert
            Assert.IsNotNull(contentResult);
            Assert.IsNotNull(contentResult.Content);
            Assert.AreEqual("Found", contentResult.Content.Id);
        }
        public void Get_ShouldReturnOkResult()
        {
            // Arrange
            var mockRepository = new Mock<IProductRepository>();
            mockRepository.Setup(x => x.GetProduct("Found"))
                .Returns(new Product { Id = "Found" });

            var controller = new ProductsController(mockRepository.Object);

            // Act
            IHttpActionResult actionResult = controller.Get("Found");            

            // Assert
            Assert.IsInstanceOfType(actionResult, typeof(OkNegotiatedContentResult<Product>));
        }
        public void Get_ShouldReturnNotFoundWhenProductDoesNotExist()
        {
            // Arrange
            var mockRepository = new Mock<IProductRepository>();
            mockRepository.Setup(x => x.GetProduct("Not Found"));

            var controller = new ProductsController(mockRepository.Object);

            // Act
            IHttpActionResult actionResult = controller.Get("Not Found");

            // Assert
            Assert.IsInstanceOfType(actionResult, typeof(NotFoundResult));
        }
        public void Get_ShouldReturnAllProducts()
        {
            // Arrange
            var mockRepository = new Mock<IProductRepository>();
            var testData = Helpers.GetTestData();
            mockRepository.Setup(x => x.GetAllProducts())
               .Returns(testData.AsQueryable());
            
            var controller = new ProductsController(mockRepository.Object);

            // Act
            IEnumerable<Product> products = controller.Get();

            // Assert
            Assert.IsNotNull(products);
            Assert.AreEqual(testData.Count, products.Count());
        }