Refactoring and Adding Tests (#58)
* Moving Identity seeding to its own class and method. * Adding tests for AddItem * Added catalog api controller and functional tests Added and cleaned up some comments * Adding integration tests for OrderRepository * Getting integration test for order working with inmemory db
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
using System.IO;
|
||||
using Xunit;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Microsoft.eShopWeb.ViewModels;
|
||||
using System.Linq;
|
||||
|
||||
namespace FunctionalTests.Web.Controllers
|
||||
{
|
||||
public class ApiCatalogControllerList : BaseWebTest
|
||||
{
|
||||
[Fact]
|
||||
public async Task ReturnsFirst10CatalogItems()
|
||||
{
|
||||
var response = await _client.GetAsync("/api/catalog/list");
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = JsonConvert.DeserializeObject<CatalogIndexViewModel>(stringResponse);
|
||||
|
||||
Assert.Equal(10, model.CatalogItems.Count());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReturnsLast2CatalogItemsGivenPageIndex1()
|
||||
{
|
||||
var response = await _client.GetAsync("/api/catalog/list?page=1");
|
||||
response.EnsureSuccessStatusCode();
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var model = JsonConvert.DeserializeObject<CatalogIndexViewModel>(stringResponse);
|
||||
|
||||
Assert.Equal(2, model.CatalogItems.Count());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,11 @@ using Microsoft.Extensions.PlatformAbstractions;
|
||||
using Microsoft.eShopWeb;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Infrastructure.Data;
|
||||
using Infrastructure.Identity;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace FunctionalTests.Web.Controllers
|
||||
{
|
||||
@@ -25,12 +30,25 @@ namespace FunctionalTests.Web.Controllers
|
||||
_contentRoot = GetProjectPath("src", startupAssembly);
|
||||
var builder = new WebHostBuilder()
|
||||
.UseContentRoot(_contentRoot)
|
||||
.UseEnvironment("Testing")
|
||||
.UseStartup<Startup>();
|
||||
|
||||
var server = new TestServer(builder);
|
||||
var client = server.CreateClient();
|
||||
|
||||
return client;
|
||||
// seed data
|
||||
using (var scope = server.Host.Services.CreateScope())
|
||||
{
|
||||
var services = scope.ServiceProvider;
|
||||
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
|
||||
var catalogContext = services.GetRequiredService<CatalogContext>();
|
||||
CatalogContextSeed.SeedAsync(catalogContext, loggerFactory)
|
||||
.Wait();
|
||||
|
||||
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
|
||||
AppIdentityDbContextSeed.SeedAsync(userManager).Wait();
|
||||
}
|
||||
|
||||
return server.CreateClient();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
using System.IO;
|
||||
using Xunit;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FunctionalTests.Web.Controllers
|
||||
{
|
||||
public class CatalogControllerGetImage : BaseWebTest
|
||||
{
|
||||
//[Fact]
|
||||
// GetImage replaced by static file middleware
|
||||
public async Task ReturnsFileContentResultGivenValidId()
|
||||
{
|
||||
var testFilePath = Path.Combine(_contentRoot, "pics//1.png");
|
||||
var expectedFileBytes = File.ReadAllBytes(testFilePath);
|
||||
|
||||
var response = await _client.GetAsync("/catalog/pic/1");
|
||||
response.EnsureSuccessStatusCode();
|
||||
var streamResponse = await response.Content.ReadAsStreamAsync();
|
||||
byte[] byteResult;
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
streamResponse.CopyTo(ms);
|
||||
byteResult = ms.ToArray();
|
||||
}
|
||||
|
||||
Assert.Equal(expectedFileBytes, byteResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user