You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

69 lines
2.4 KiB

using Microsoft.AspNetCore.Mvc;
using Microsoft.eShopOnContainers.Services.Basket.API.Controllers;
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
using Moq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;
namespace UnitTest.Basket.Application
{
public class BasketWebApiTest
{
private readonly Mock<IBasketRepository> _basketRepositoryMock;
public BasketWebApiTest()
{
_basketRepositoryMock = new Mock<IBasketRepository>();
}
[Fact]
public async Task Get_customer_basket_success()
{
//Arrange
var fakeCustomerId = "1";
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
_basketRepositoryMock.Setup(x => x.GetBasketAsync(It.IsAny<string>()))
.Returns(Task.FromResult(fakeCustomerBasket));
//Act
var basketController = new BasketController(_basketRepositoryMock.Object);
var actionResult = await basketController.Get(fakeCustomerId) as OkObjectResult;
//Assert
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
}
[Fact]
public async Task Post_customer_basket_success()
{
//Arrange
var fakeCustomerId = "1";
var fakeCustomerBasket = GetCustomerBasketFake(fakeCustomerId);
_basketRepositoryMock.Setup(x => x.UpdateBasketAsync(It.IsAny<CustomerBasket>()))
.Returns(Task.FromResult(fakeCustomerBasket));
//Act
var basketController = new BasketController(_basketRepositoryMock.Object);
var actionResult = await basketController.Post(fakeCustomerBasket) as OkObjectResult;
//Assert
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
}
private CustomerBasket GetCustomerBasketFake(string fakeCustomerId)
{
return new CustomerBasket(fakeCustomerId)
{
Items = new List<BasketItem>()
{
new BasketItem()
}
};
}
}
}