Skip to content

Unit tests todoItemService #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 185 additions & 0 deletions TodoList.UnitTests/Services/TodoItemServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
using Microsoft.EntityFrameworkCore;
using Moq;
using NodaTime;
using System;
using System.Threading.Tasks;
using TodoList.Web.Data;
using TodoList.Web.Models;
using TodoList.Web.Services;
using Xunit;

namespace TodoList.UnitTests.Services
{
public class TodoItemServiceTest
{
private readonly Mock<IClock> _clockMock;

public TodoItemServiceTest()
{
_clockMock = new Mock<IClock>(MockBehavior.Strict);

_clockMock
.Setup(clock => clock.GetCurrentInstant())
.Returns(new Instant());
}

[Fact]
public async Task AddItemAsync_ReturnsTrueIfSucceeds()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.Options;
var user = new ApplicationUser() { Id = Guid.NewGuid().ToString() };
var todoItem = new TodoItem()
{
Title = "Cleaning"
};

bool success;
using (var context = new ApplicationDbContext(options))
{
var tester = new TodoItemService(context, _clockMock.Object);
success = await tester.AddItemAsync(todoItem, user);
}

Assert.True(success);
}

[Fact]
public async Task AddItemAsync_SetsId()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.Options;
var user = new ApplicationUser() { Id = Guid.NewGuid().ToString() };
var todoItem = new TodoItem()
{
Title = "Cleaning"
};

using (var context = new ApplicationDbContext(options))
{
var tester = new TodoItemService(context, _clockMock.Object);
await tester.AddItemAsync(todoItem, user);
}

using (var context = new ApplicationDbContext(options))
{
var tester = new TodoItemService(context, _clockMock.Object);
var result = await context.Todos
.SingleAsync(todo => todo.Title.Equals(todoItem.Title));

Assert.True(result.Id.ToString().Length.Equals(36));
}
}

[Fact]
public async Task AddItemAsync_SetsDoneToFalse()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.Options;
var user = new ApplicationUser() { Id = Guid.NewGuid().ToString() };
var todoItem = new TodoItem()
{
Title = "Cleaning"
};

using (var context = new ApplicationDbContext(options))
{
var tester = new TodoItemService(context, _clockMock.Object);
await tester.AddItemAsync(todoItem, user);
}

using (var context = new ApplicationDbContext(options))
{
var tester = new TodoItemService(context, _clockMock.Object);
var result = await context.Todos
.SingleAsync(todo => todo.Title.Equals(todoItem.Title));

Assert.False(result.Done);
}
}

[Fact]
public async Task AddItemAsync_UsesClock()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.Options;
var user = new ApplicationUser() { Id = Guid.NewGuid().ToString() };
var todoItem = new TodoItem()
{
Title = "Cleaning"
};

using (var context = new ApplicationDbContext(options))
{
var tester = new TodoItemService(context, _clockMock.Object);
await tester.AddItemAsync(todoItem, user);
}

_clockMock
.Verify(clock => clock.GetCurrentInstant(), Times.Once);
}

[Fact]
public async Task AddItemAsync_SetsUserId()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.Options;
var user = new ApplicationUser() { Id = Guid.NewGuid().ToString() };
var todoItem = new TodoItem()
{
Title = "Cleaning"
};

using (var context = new ApplicationDbContext(options))
{
var tester = new TodoItemService(context, _clockMock.Object);
await tester.AddItemAsync(todoItem, user);
}

using (var context = new ApplicationDbContext(options))
{
var tester = new TodoItemService(context, _clockMock.Object);
var result = await context.Todos
.SingleAsync(todo => todo.Title.Equals(todoItem.Title));

Assert.Equal(user.Id, result.UserId);
}
}

[Fact]
public async Task AddItemAsync_SetsDefaultValuesForFile()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.Options;
var user = new ApplicationUser() { Id = Guid.NewGuid().ToString() };
var todoItem = new TodoItem()
{
Title = "Cleaning"
};

using (var context = new ApplicationDbContext(options))
{
var tester = new TodoItemService(context, _clockMock.Object);
await tester.AddItemAsync(todoItem, user);
}

using (var context = new ApplicationDbContext(options))
{
var tester = new TodoItemService(context, _clockMock.Object);
var result = await context.Todos
.Include(todo => todo.File)
.SingleAsync(todo => todo.Title.Equals(todoItem.Title));

Assert.Equal(result.Id, result.File.TodoId);
Assert.Equal(string.Empty, result.File.Path);
Assert.Equal(0, result.File.Size);
}
}
}
}
7 changes: 0 additions & 7 deletions TodoList.UnitTests/TodoItemServiceShould.cs

This file was deleted.

18 changes: 11 additions & 7 deletions TodoList.Web/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
ApplyMigrations(this);
ApplyMigrations(this);
}

public DbSet<TodoItem> Todos { get; set; }
Expand All @@ -25,12 +25,16 @@ protected override void OnModelCreating(ModelBuilder builder)
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}

public void ApplyMigrations(ApplicationDbContext context) {
if (context.Database.GetPendingMigrations().Any()) {
context.Database.Migrate();
}
}

public void ApplyMigrations(ApplicationDbContext context)
{
if (context.Database.IsSqlServer())
{
if (context.Database.GetPendingMigrations().Any())
{
context.Database.Migrate();
}
}
}
}
}