fix: добавил в индексацию проект с тестами.

parent 6e40b765
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using PhoneStore.Enums;
using PhoneStore.Models;
using PhoneStore.Tests.Helpers;
using PhoneStore.ViewModels.PhoneViewModels;
using Xunit;
using StatusCodes = Microsoft.AspNetCore.Http.StatusCodes;
namespace PhoneStore.Tests
{
public class PhoneControllerTests
{
private HttpClient _httpClient;
private MobileContext _context;
public PhoneControllerTests()
{
var webHost = Utilities.SubstituteDbOnTestDb();
_httpClient = webHost.CreateClient();
_context = webHost.Services.CreateScope().ServiceProvider.GetService<MobileContext>();
}
[Fact]
public async Task Send_Create_Phone_Returns_Success()
{
var phone = new PhoneCreateViewModel
{
Name = "Test",
Price = 3000M,
BrandId = Utilities.CreateBrandAndReturnBrandId(_context)
};
string json = JsonSerializer.Serialize(phone);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync($"Phones/Create", content);
response.StatusCode.Should().Be(StatusCodes.Status200OK);
}
[Fact]
public async Task Send_Create_Phone_With_NotValidName_Returns_BadRequest()
{
var phone = new PhoneCreateViewModel
{
Name = null,
Price = 0,
BrandId = Utilities.CreateBrandAndReturnBrandId(_context)
};
string json = JsonSerializer.Serialize(phone);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync($"Phones/Create", content);
response.StatusCode.Should().Be(StatusCodes.Status400BadRequest);
}
[Fact]
public async Task Send_Create_Phone_With_NotValidPrice_Returns_ValidationProblem()
{
var phone = new PhoneCreateViewModel
{
Name = "Test",
Price = 0,
BrandId = Utilities.CreateBrandAndReturnBrandId(_context)
};
string json = JsonSerializer.Serialize(phone);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync($"Phones/Create", content);
response.StatusCode.Should().Be(StatusCodes.Status400BadRequest);
}
[Fact]
public async Task Send_Create_Phone_With_NegativePrice_Returns_ValidationProblem()
{
var phone = new PhoneCreateViewModel
{
Name = "Test",
Price = -3000M,
BrandId = Utilities.CreateBrandAndReturnBrandId(_context)
};
string json = JsonSerializer.Serialize(phone);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync($"Phones/Create", content);
response.StatusCode.Should().Be(StatusCodes.Status400BadRequest);
}
[Fact]
public async Task Get_Phone_By_Id_Returns_PhoneVM()
{
var phoneId = Utilities.CreatePhoneAndReturnPhoneId(_context);
HttpResponseMessage response = await _httpClient.GetAsync($"Phones/About?phoneId={phoneId}");
var jsonString = await response.Content.ReadAsStringAsync();
var phoneVm = JsonSerializer.Deserialize<PhoneViewModel>(jsonString);
response.StatusCode.Should().Be(StatusCodes.Status200OK);
phoneVm.Should().NotBeNull();
phoneVm.Brand.Should().NotBeNull();
phoneVm.Id.Should().Be(phoneId);
}
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.17" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PhoneStore\PhoneStore.csproj" />
</ItemGroup>
</Project>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment