Browse Source

Migrated some tests to use System.Text.Json (#385)

* Migrated some tests to use System.Text.Json

* Added case insensitive json options to fix broken tests.
main
Steve Smith 6 years ago
committed by GitHub
parent
commit
3030e97af6
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/Web/Services/CatalogViewModelService.cs
  2. 6
      src/Web/ViewModels/CatalogIndexViewModel.cs
  3. 1
      src/Web/Web.csproj
  4. 8
      tests/FunctionalTests/Web/Controllers/ApiCatalogControllerList.cs

6
src/Web/Services/CatalogViewModelService.cs

@ -57,9 +57,9 @@ namespace Microsoft.eShopWeb.Web.Services
Name = i.Name, Name = i.Name,
PictureUri = _uriComposer.ComposePicUri(i.PictureUri), PictureUri = _uriComposer.ComposePicUri(i.PictureUri),
Price = i.Price Price = i.Price
}), }).ToList(),
Brands = await GetBrands(), Brands = (await GetBrands()).ToList(),
Types = await GetTypes(), Types = (await GetTypes()).ToList(),
BrandFilterApplied = brandId ?? 0, BrandFilterApplied = brandId ?? 0,
TypesFilterApplied = typeId ?? 0, TypesFilterApplied = typeId ?? 0,
PaginationInfo = new PaginationInfoViewModel() PaginationInfo = new PaginationInfoViewModel()

6
src/Web/ViewModels/CatalogIndexViewModel.cs

@ -5,9 +5,9 @@ namespace Microsoft.eShopWeb.Web.ViewModels
{ {
public class CatalogIndexViewModel public class CatalogIndexViewModel
{ {
public IEnumerable<CatalogItemViewModel> CatalogItems { get; set; } public List<CatalogItemViewModel> CatalogItems { get; set; }
public IEnumerable<SelectListItem> Brands { get; set; } public List<SelectListItem> Brands { get; set; }
public IEnumerable<SelectListItem> Types { get; set; } public List<SelectListItem> Types { get; set; }
public int? BrandFilterApplied { get; set; } public int? BrandFilterApplied { get; set; }
public int? TypesFilterApplied { get; set; } public int? TypesFilterApplied { get; set; }
public PaginationInfoViewModel PaginationInfo { get; set; } public PaginationInfoViewModel PaginationInfo { get; set; }

1
src/Web/Web.csproj

@ -34,6 +34,7 @@
</PackageReference> </PackageReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Catalog\" />
<Folder Include="wwwroot\fonts\" /> <Folder Include="wwwroot\fonts\" />
<Folder Include="wwwroot\lib\" /> <Folder Include="wwwroot\lib\" />
</ItemGroup> </ItemGroup>

8
tests/FunctionalTests/Web/Controllers/ApiCatalogControllerList.cs

@ -1,7 +1,7 @@
using Microsoft.eShopWeb.Web.ViewModels; using Microsoft.eShopWeb.Web.ViewModels;
using Newtonsoft.Json;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
using Xunit; using Xunit;
@ -10,6 +10,8 @@ namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers
[Collection("Sequential")] [Collection("Sequential")]
public class ApiCatalogControllerList : IClassFixture<WebTestFixture> public class ApiCatalogControllerList : IClassFixture<WebTestFixture>
{ {
JsonSerializerOptions _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
public ApiCatalogControllerList(WebTestFixture factory) public ApiCatalogControllerList(WebTestFixture factory)
{ {
Client = factory.CreateClient(); Client = factory.CreateClient();
@ -23,7 +25,7 @@ namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers
var response = await Client.GetAsync("/api/catalog/list"); var response = await Client.GetAsync("/api/catalog/list");
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync(); var stringResponse = await response.Content.ReadAsStringAsync();
var model = JsonConvert.DeserializeObject<CatalogIndexViewModel>(stringResponse); var model = JsonSerializer.Deserialize<CatalogIndexViewModel>(stringResponse, _jsonOptions);
Assert.Equal(10, model.CatalogItems.Count()); Assert.Equal(10, model.CatalogItems.Count());
} }
@ -34,7 +36,7 @@ namespace Microsoft.eShopWeb.FunctionalTests.Web.Controllers
var response = await Client.GetAsync("/api/catalog/list?page=1"); var response = await Client.GetAsync("/api/catalog/list?page=1");
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync(); var stringResponse = await response.Content.ReadAsStringAsync();
var model = JsonConvert.DeserializeObject<CatalogIndexViewModel>(stringResponse); var model = JsonSerializer.Deserialize<CatalogIndexViewModel>(stringResponse, _jsonOptions);
Assert.Equal(2, model.CatalogItems.Count()); Assert.Equal(2, model.CatalogItems.Count());
} }

Loading…
Cancel
Save