Commit bdcf4d49 authored by Scott DePouw's avatar Scott DePouw Committed by Steve Smith

Test todo builder (#17)

* Changed class to match filename.

* Implement Builder Pattern for ToDoItems in tests, and adjust EfRepositoryShould to use it.

* Move test utility classes to root of Tests project.

* Using ToDoItemBuilder in other ToDo tests.

* Complete ToDoItemBuilder by adding other ToDoItem field methods.
parent d51fca4a
using CleanArchitecture.Core.Entities; using CleanArchitecture.Core.Events;
using CleanArchitecture.Core.Events;
using System.Linq; using System.Linq;
using Xunit; using Xunit;
...@@ -10,7 +9,7 @@ namespace CleanArchitecture.Tests.Core.Entities ...@@ -10,7 +9,7 @@ namespace CleanArchitecture.Tests.Core.Entities
[Fact] [Fact]
public void SetIsDoneToTrue() public void SetIsDoneToTrue()
{ {
var item = new ToDoItem(); var item = new ToDoItemBuilder().Build();
item.MarkComplete(); item.MarkComplete();
...@@ -20,7 +19,7 @@ namespace CleanArchitecture.Tests.Core.Entities ...@@ -20,7 +19,7 @@ namespace CleanArchitecture.Tests.Core.Entities
[Fact] [Fact]
public void RaiseToDoItemCompletedEvent() public void RaiseToDoItemCompletedEvent()
{ {
var item = new ToDoItem(); var item = new ToDoItemBuilder().Build();
item.MarkComplete(); item.MarkComplete();
......
using System; using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Infrastructure.Data;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Xunit;
using CleanArchitecture.Infrastructure.Data;
using CleanArchitecture.Core.Entities;
using System.Linq;
using CleanArchitecture.Core.Events;
using CleanArchitecture.Core.Interfaces;
using Moq; using Moq;
using System;
using System.Linq;
using Xunit;
namespace CleanArchitecture.Tests.Integration.Data namespace CleanArchitecture.Tests.Integration.Data
{ {
public class EfRepositoryAddShould public class EfRepositoryShould
{ {
private AppDbContext _dbContext; 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
// InMemory database instance. // InMemory database instance.
var serviceProvider = new ServiceCollection() var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase() .AddEntityFrameworkInMemoryDatabase()
...@@ -36,15 +35,14 @@ namespace CleanArchitecture.Tests.Integration.Data ...@@ -36,15 +35,14 @@ namespace CleanArchitecture.Tests.Integration.Data
public void AddItemAndSetId() public void AddItemAndSetId()
{ {
var repository = GetRepository(); var repository = GetRepository();
var item = new ToDoItem(); var item = new ToDoItemBuilder().Build();
repository.Add(item); repository.Add(item);
var newItem = repository.List().FirstOrDefault(); var newItem = repository.List().FirstOrDefault();
Assert.Equal(item, newItem); Assert.Equal(item, newItem);
Assert.True(newItem.Id > 0); Assert.True(newItem?.Id > 0);
} }
[Fact] [Fact]
...@@ -53,27 +51,27 @@ namespace CleanArchitecture.Tests.Integration.Data ...@@ -53,27 +51,27 @@ namespace CleanArchitecture.Tests.Integration.Data
// add an item // add an item
var repository = GetRepository(); var repository = GetRepository();
var initialTitle = Guid.NewGuid().ToString(); var initialTitle = Guid.NewGuid().ToString();
var item = new ToDoItem() var item = new ToDoItemBuilder().Title(initialTitle).Build();
{
Title = initialTitle
};
repository.Add(item); repository.Add(item);
// detach the item so we get a different instance // detach the item so we get a different instance
_dbContext.Entry(item).State = EntityState.Detached; _dbContext.Entry(item).State = EntityState.Detached;
// fetch the item and update its title // fetch the item and update its title
var newItem = repository.List() var newItem = repository.List()
.FirstOrDefault(i => i.Title == initialTitle); .FirstOrDefault(i => i.Title == initialTitle);
Assert.NotNull(newItem);
Assert.NotSame(item, newItem); Assert.NotSame(item, newItem);
var newTitle = Guid.NewGuid().ToString(); var newTitle = Guid.NewGuid().ToString();
newItem.Title = newTitle; newItem.Title = newTitle;
// Update the item // Update the item
repository.Update(newItem); repository.Update(newItem);
var updatedItem = repository.List() var updatedItem = repository.List()
.FirstOrDefault(i => i.Title == newTitle); .FirstOrDefault(i => i.Title == newTitle);
Assert.NotNull(updatedItem);
Assert.NotEqual(item.Title, updatedItem.Title); Assert.NotEqual(item.Title, updatedItem.Title);
Assert.Equal(newItem.Id, updatedItem.Id); Assert.Equal(newItem.Id, updatedItem.Id);
} }
...@@ -84,22 +82,17 @@ namespace CleanArchitecture.Tests.Integration.Data ...@@ -84,22 +82,17 @@ namespace CleanArchitecture.Tests.Integration.Data
// add an item // add an item
var repository = GetRepository(); var repository = GetRepository();
var initialTitle = Guid.NewGuid().ToString(); var initialTitle = Guid.NewGuid().ToString();
var item = new ToDoItem() var item = new ToDoItemBuilder().Title(initialTitle).Build();
{
Title = initialTitle
};
repository.Add(item); repository.Add(item);
// delete the item // delete the item
repository.Delete(item); repository.Delete(item);
// verify it's no longer there // verify it's no longer there
Assert.DoesNotContain(repository.List(), Assert.DoesNotContain(repository.List(),
i => i.Title == initialTitle); i => i.Title == initialTitle);
} }
private EfRepository<ToDoItem> GetRepository() private EfRepository<ToDoItem> GetRepository()
{ {
var options = CreateNewContextOptions(); var options = CreateNewContextOptions();
...@@ -109,4 +102,4 @@ namespace CleanArchitecture.Tests.Integration.Data ...@@ -109,4 +102,4 @@ namespace CleanArchitecture.Tests.Integration.Data
return new EfRepository<ToDoItem>(_dbContext); return new EfRepository<ToDoItem>(_dbContext);
} }
} }
} }
\ No newline at end of file
using CleanArchitecture.Core.Interfaces; using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Core.SharedKernel; using CleanArchitecture.Core.SharedKernel;
namespace CleanArchitecture.Tests.Integration.Web namespace CleanArchitecture.Tests
{ {
public class NoOpDomainEventDispatcher : IDomainEventDispatcher public class NoOpDomainEventDispatcher : IDomainEventDispatcher
{ {
......
using CleanArchitecture.Core.Entities;
namespace CleanArchitecture.Tests
{
public class ToDoItemBuilder
{
private readonly ToDoItem _todo = new ToDoItem();
public ToDoItemBuilder Id(int id)
{
_todo.Id = id;
return this;
}
public ToDoItemBuilder Title(string title)
{
_todo.Title = title;
return this;
}
public ToDoItemBuilder Description(string description)
{
_todo.Description = description;
return this;
}
public ToDoItem Build() => _todo;
}
}
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