Commit c841e0c4 authored by Steve Smith's avatar Steve Smith

Reorganizing and adding more tests

parent 61436fa4
using CleanArchitecture.Core.Events; using CleanArchitecture.Core.Events;
using CleanArchitecture.Core.SharedKernel;
namespace CleanArchitecture.Core.Model namespace CleanArchitecture.Core.Entities
{ {
public class ToDoItem : BaseEntity public class ToDoItem : BaseEntity
{ {
......
using CleanArchitecture.Core.Model; using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Model;
namespace CleanArchitecture.Core.Events namespace CleanArchitecture.Core.Events
{ {
......
using System.Collections.Generic; using System.Collections.Generic;
using CleanArchitecture.Core.Model; using CleanArchitecture.Core.Model;
using CleanArchitecture.Core.SharedKernel;
namespace CleanArchitecture.Core.Interfaces namespace CleanArchitecture.Core.Interfaces
{ {
......
using CleanArchitecture.Core.Events;
using CleanArchitecture.Core.Interfaces;
namespace CleanArchitecture.Core.Services
{
public class ToDoItemService : IHandle<ToDoItemCompletedEvent>
{
public void Handle(ToDoItemCompletedEvent domainEvent)
{
// Do Nothing
}
}
}
using System.Collections.Generic; using System.Collections.Generic;
using CleanArchitecture.Core.Model;
namespace CleanArchitecture.Core.Model namespace CleanArchitecture.Core.SharedKernel
{ {
// This can be modified to BaseEntity<TId> to support multiple key types (e.g. Guid) // This can be modified to BaseEntity<TId> to support multiple key types (e.g. Guid)
public abstract class BaseEntity public abstract class BaseEntity
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
using CleanArchitecture.Core.Model; using CleanArchitecture.Core.Model;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Linq; using System.Linq;
using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.SharedKernel;
namespace CleanArchitecture.Infrastructure.Data namespace CleanArchitecture.Infrastructure.Data
{ {
......
...@@ -2,6 +2,7 @@ using System.Collections.Generic; ...@@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using CleanArchitecture.Core.Interfaces; using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Core.Model; using CleanArchitecture.Core.Model;
using CleanArchitecture.Core.SharedKernel;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace CleanArchitecture.Infrastructure.Data namespace CleanArchitecture.Infrastructure.Data
...@@ -27,17 +28,6 @@ namespace CleanArchitecture.Infrastructure.Data ...@@ -27,17 +28,6 @@ namespace CleanArchitecture.Infrastructure.Data
public T Add(T entity) public T Add(T entity)
{ {
// if using in memory EF, need to support IDENTITY keys
//if (entity.Id == 0)
//{
// int newId = 1;
// var entities = List();
// if (entities.Any())
// {
// newId = entities.Max(z => z.Id) + 1;
// }
// entity.Id = newId;
//}
_dbContext.Set<T>().Add(entity); _dbContext.Set<T>().Add(entity);
_dbContext.SaveChanges(); _dbContext.SaveChanges();
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Interfaces;
using Microsoft.AspNetCore.Mvc;
namespace CleanArchitecture.Web.Controllers
{
public class ToDoController : Controller
{
private readonly IRepository<ToDoItem> _todoRepository;
public ToDoController(IRepository<ToDoItem> todoRepository)
{
_todoRepository = todoRepository;
}
public IActionResult Index()
{
return View();
}
}
}
@{ @{
ViewData["Title"] = "Home Page"; ViewData["Title"] = "Clean DDD Architecture Sample";
} }
<div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="6000">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
<li data-target="#myCarousel" data-slide-to="3"></li>
</ol>
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="~/images/banner1.svg" alt="ASP.NET" class="img-responsive" />
<div class="carousel-caption" role="option">
<p>
Learn how to build ASP.NET apps that can run anywhere.
<a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkID=525028&clcid=0x409">
Learn More
</a>
</p>
</div>
</div>
<div class="item">
<img src="~/images/banner2.svg" alt="Visual Studio" class="img-responsive" />
<div class="carousel-caption" role="option">
<p>
There are powerful new features in Visual Studio for building modern web apps.
<a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkID=525030&clcid=0x409">
Learn More
</a>
</p>
</div>
</div>
<div class="item">
<img src="~/images/banner3.svg" alt="Package Management" class="img-responsive" />
<div class="carousel-caption" role="option">
<p>
Bring in libraries from NuGet, Bower, and npm, and automate tasks using Grunt or Gulp.
<a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkID=525029&clcid=0x409">
Learn More
</a>
</p>
</div>
</div>
<div class="item">
<img src="~/images/banner4.svg" alt="Microsoft Azure" class="img-responsive" />
<div class="carousel-caption" role="option">
<p>
Learn how Microsoft's Azure cloud platform allows you to build, deploy, and scale web apps.
<a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkID=525027&clcid=0x409">
Learn More
</a>
</p>
</div>
</div>
</div>
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<div class="row"> <div class="row">
<div class="col-md-3"> <div class="col-md-6">
<h2>Application uses</h2> <h2>To Do Items</h2>
<ul> <ul>
<li>Sample pages using ASP.NET Core MVC</li> <li><a asp-area="" asp-controller="ToDo" asp-action="Index">To Do Items</a></li>
<li><a href="http://go.microsoft.com/fwlink/?LinkId=518004">Bower</a> for managing client-side libraries</li>
<li>Theming using <a href="http://go.microsoft.com/fwlink/?LinkID=398939">Bootstrap</a></li>
</ul> </ul>
</div> </div>
<div class="col-md-3"> <div class="col-md-6">
<h2>How to</h2> <h2>How to</h2>
<ul> <ul>
<li><a href="http://go.microsoft.com/fwlink/?LinkID=398600">Add a Controller and View</a></li> <li><a href="http://go.microsoft.com/fwlink/?LinkID=398600">Add a Controller and View</a></li>
...@@ -86,24 +21,4 @@ ...@@ -86,24 +21,4 @@
<li><a href="http://go.microsoft.com/fwlink/?LinkId=699319">Target development, staging or production environment.</a></li> <li><a href="http://go.microsoft.com/fwlink/?LinkId=699319">Target development, staging or production environment.</a></li>
</ul> </ul>
</div> </div>
<div class="col-md-3">
<h2>Overview</h2>
<ul>
<li><a href="http://go.microsoft.com/fwlink/?LinkId=518008">Conceptual overview of what is ASP.NET Core</a></li>
<li><a href="http://go.microsoft.com/fwlink/?LinkId=699320">Fundamentals of ASP.NET Core such as Startup and middleware.</a></li>
<li><a href="http://go.microsoft.com/fwlink/?LinkId=398602">Working with Data</a></li>
<li><a href="http://go.microsoft.com/fwlink/?LinkId=398603">Security</a></li>
<li><a href="http://go.microsoft.com/fwlink/?LinkID=699321">Client side development</a></li>
<li><a href="http://go.microsoft.com/fwlink/?LinkID=699322">Develop on different platforms</a></li>
<li><a href="http://go.microsoft.com/fwlink/?LinkID=699323">Read more on the documentation site</a></li>
</ul>
</div>
<div class="col-md-3">
<h2>Run & Deploy</h2>
<ul>
<li><a href="http://go.microsoft.com/fwlink/?LinkID=517851">Run your app</a></li>
<li><a href="http://go.microsoft.com/fwlink/?LinkID=517853">Run tools such as EF migrations and more</a></li>
<li><a href="http://go.microsoft.com/fwlink/?LinkID=398609">Publish to Microsoft Azure Web Apps</a></li>
</ul>
</div>
</div> </div>
using CleanArchitecture.Core.Model; using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Events; using CleanArchitecture.Core.Events;
using System.Linq; using System.Linq;
using Xunit; using Xunit;
namespace CleanArchitecture.Tests.Core.Model namespace CleanArchitecture.Tests.Core.Entities
{ {
public class ToDoItemMarkCompleteShould public class ToDoItemMarkCompleteShould
{ {
......
using Microsoft.EntityFrameworkCore; using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Xunit; using Xunit;
using CleanArchitecture.Infrastructure.Data; using CleanArchitecture.Infrastructure.Data;
using CleanArchitecture.Core.Model; using CleanArchitecture.Core.Entities;
using System.Linq; using System.Linq;
using CleanArchitecture.Core.Events; using CleanArchitecture.Core.Events;
using CleanArchitecture.Core.Interfaces; using CleanArchitecture.Core.Interfaces;
...@@ -12,6 +13,8 @@ namespace CleanArchitecture.Tests.Integration.Data ...@@ -12,6 +13,8 @@ namespace CleanArchitecture.Tests.Integration.Data
{ {
public class EfRepositoryAddShould public class EfRepositoryAddShould
{ {
private AppDbContext _dbContext;
private static DbContextOptions<AppDbContext> CreateNewContextOptions() private static DbContextOptions<AppDbContext> CreateNewContextOptions()
{ {
// Create a fresh service provider, and therefore a fresh // Create a fresh service provider, and therefore a fresh
...@@ -28,6 +31,7 @@ namespace CleanArchitecture.Tests.Integration.Data ...@@ -28,6 +31,7 @@ namespace CleanArchitecture.Tests.Integration.Data
return builder.Options; return builder.Options;
} }
[Fact] [Fact]
public void AddItemAndSetId() public void AddItemAndSetId()
{ {
...@@ -43,12 +47,66 @@ namespace CleanArchitecture.Tests.Integration.Data ...@@ -43,12 +47,66 @@ namespace CleanArchitecture.Tests.Integration.Data
} }
[Fact]
public void UpdateItemAfterAddingIt()
{
// add an item
var repository = GetRepository();
var initialTitle = Guid.NewGuid().ToString();
var item = new ToDoItem()
{
Title = initialTitle
};
repository.Add(item);
// detach the item so we get a different instance
_dbContext.Entry(item).State = EntityState.Detached;
// fetch the item and update its title
var newItem = repository.List()
.FirstOrDefault(i => i.Title == initialTitle);
Assert.NotSame(item, newItem);
var newTitle = Guid.NewGuid().ToString();
newItem.Title = newTitle;
// Update the item
repository.Update(newItem);
var updatedItem = repository.List()
.FirstOrDefault(i => i.Title == newTitle);
Assert.NotEqual(item.Title, updatedItem.Title);
Assert.Equal(newItem.Id, updatedItem.Id);
}
[Fact]
public void DeleteItemAfterAddingIt()
{
// add an item
var repository = GetRepository();
var initialTitle = Guid.NewGuid().ToString();
var item = new ToDoItem()
{
Title = initialTitle
};
repository.Add(item);
// delete the item
repository.Delete(item);
// verify it's no longer there
Assert.DoesNotContain(repository.List(),
i => i.Title == initialTitle);
}
private EfRepository<ToDoItem> GetRepository() private EfRepository<ToDoItem> GetRepository()
{ {
var options = CreateNewContextOptions(); var options = CreateNewContextOptions();
var mockDispatcher = new Mock<IDomainEventDispatcher>(); var mockDispatcher = new Mock<IDomainEventDispatcher>();
return new EfRepository<ToDoItem>(new AppDbContext(options, mockDispatcher.Object)); _dbContext = new AppDbContext(options, mockDispatcher.Object);
return new EfRepository<ToDoItem>(_dbContext);
} }
} }
} }
\ No newline at end of file
...@@ -12,7 +12,9 @@ ...@@ -12,7 +12,9 @@
"System.Diagnostics.TraceSource": "4.0.0", "System.Diagnostics.TraceSource": "4.0.0",
"Moq": "4.6.25-alpha" "Moq": "4.6.25-alpha"
}, },
"tools": {
"Microsoft.DotNet.Watcher.Tools": "1.0.0-preview2-final"
},
"frameworks": { "frameworks": {
"netcoreapp1.0": { "netcoreapp1.0": {
"imports": [ "imports": [
......
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