https://github.com/nosratifarhad/integrationtest_xunit
Integration test Use XUnit Use Bogus And HttpClient
https://github.com/nosratifarhad/integrationtest_xunit
bogus dotnet dotnet-core dotnetcore faker httpclient integration integration-testing integrationtest
Last synced: about 1 month ago
JSON representation
Integration test Use XUnit Use Bogus And HttpClient
- Host: GitHub
- URL: https://github.com/nosratifarhad/integrationtest_xunit
- Owner: nosratifarhad
- Created: 2023-04-13T14:45:57.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-11-30T12:13:40.000Z (over 2 years ago)
- Last Synced: 2025-06-24T08:43:21.108Z (12 months ago)
- Topics: bogus, dotnet, dotnet-core, dotnetcore, faker, httpclient, integration, integration-testing, integrationtest
- Language: C#
- Homepage:
- Size: 139 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# IntegrationTest In Xunit
* Test From ApiControllers Services To Repositories .
* Test Exceptions .
* Test All Logics in Services .
* Test All Validation .
----
### * Install pakeges :
* dotnet add package FluentAssertions
* dotnet add package Bogus
## I Use "FluentAssertions" For Handle Assert Tests
* You can See All Sample "FluentAssertions" In [This Repository](https://github.com/nosratifarhad/FluentAssertions.git/).
----
### Test For Created Successed Product
```csharp
[Fact]
public async Task When_ValidCreateProductInputModelInCreateProduct_Then_CreatedProductInDataBase()
{
var mockInputModel = ProductMockData.ValidCreateProductInputModel().ToRequestModel();
var response = await _client.PostAsync("/api/product", mockInputModel).ConfigureAwait(false);
response.StatusCode.Should().Be(System.Net.HttpStatusCode.Created);
response.IsSuccessStatusCode.Should().BeTrue();
var productId = await response.ToRequestItem("ProductId");
var newProductViewModel = await GetProductByResponseHeadersLocation(response.Headers.Location.AbsoluteUri).ConfigureAwait(false);
newProductViewModel.Should().NotBeNull();
newProductViewModel.ProductId.Should().Be(productId);
}
```
### Test For Update Successed Product
```csharp
[Fact]
public async Task When_ValidUpdateProductInputModelInUpdateProduct_Then_UpdateProductInDataBase()
{
var validUpdateProductInputModel = ProductMockData.ValidUpdateProductInputModel();
int productId = validUpdateProductInputModel.ProductId;
var mockInputModel = validUpdateProductInputModel.ToRequestModel();
var response = await _client.PutAsync($"/api/product/{productId}", mockInputModel).ConfigureAwait(false);
response.StatusCode.Should().Be(System.Net.HttpStatusCode.NoContent);
response.IsSuccessStatusCode.Should().BeTrue();
var newProductViewModel = await GetProductByProductId(productId).ConfigureAwait(false);
newProductViewModel.Should().NotBeNull();
newProductViewModel.ProductId.Should().Be(productId);
}
```
### Test For Get All Products
```csharp
[Fact]
public async Task When_CallGetProducts_Then_ReturnListPoductViewModel()
{
var response = await _client.GetAsync("/api/products").ConfigureAwait(false);
var responseModel = await response.ToResponseModel>();
response.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
response.IsSuccessStatusCode.Should().BeTrue();
responseModel.Should().NotBeNull();
responseModel.Should().HaveCount(5);
}
```
### Test For Get By ProductId
```csharp
[Fact]
public async Task When_ValidProductIdInGetProduct_Then_ReturnedProductViewModel()
{
int productId = await GetValidProductId();
var response = await _client.GetAsync($"/api/product/{productId}").ConfigureAwait(false);
response.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
response.IsSuccessStatusCode.Should().BeTrue();
var responseModel = await response.ToResponseModel();
responseModel.Should().NotBeNull();
responseModel.ProductId.Should().Be(productId);
}
```
### User Bogus For Generate Fake Data In MockDatas Like This
```csharp
public static UpdateProductInputModel ValidUpdateProductInputModel()
=> new Faker()
.RuleFor(bp => bp.ProductId, f => f.Random.Number(1, 5))
.RuleFor(bp => bp.ProductName, f => f.Name.FirstName())
.RuleFor(bp => bp.ProductTitle, f => f.Name.JobTitle())
.RuleFor(bp => bp.ProductDescription, f => f.Name.JobDescriptor())
.RuleFor(bp => bp.ProductCategory, f => f.Random.Enum())
.RuleFor(bp => bp.MainImageName, f => f.Name.FullName())
.RuleFor(bp => bp.MainImageTitle, f => f.Name.FullName())
.RuleFor(bp => bp.MainImageUri, f => f.Name.FullName())
.RuleFor(bp => bp.Color, f => f.Random.Enum())
.RuleFor(bp => bp.IsFreeDelivery, f => f.Random.Bool())
.RuleFor(bp => bp.IsExisting, f => f.Random.Bool())
.RuleFor(bp => bp.Weight, f => f.Random.Number());
```
### Passed All Tests
