Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests to bookstore angular sample
- Loading branch information
Showing
6 changed files
with
112 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions
72
...Angular-MongoDb/aspnet-core/test/Acme.BookStore.Application.Tests/BookAppService_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Shouldly; | ||
using Volo.Abp.Application.Dtos; | ||
using Volo.Abp.Validation; | ||
using Xunit; | ||
|
||
namespace Acme.BookStore | ||
{ | ||
public class BookAppService_Tests : BookStoreApplicationTestBase | ||
{ | ||
private readonly IBookAppService _bookAppService; | ||
|
||
public BookAppService_Tests() | ||
{ | ||
_bookAppService = GetRequiredService<IBookAppService>(); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_Get_List_Of_Books() | ||
{ | ||
//Act | ||
var result = await _bookAppService.GetListAsync( | ||
new PagedAndSortedResultRequestDto() | ||
); | ||
|
||
//Assert | ||
result.TotalCount.ShouldBeGreaterThan(0); | ||
result.Items.ShouldContain(b => b.Name == "Test book 1"); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_Create_A_Valid_Book() | ||
{ | ||
//Act | ||
var result = await _bookAppService.CreateAsync( | ||
new CreateUpdateBookDto | ||
{ | ||
Name = "New test book 42", | ||
Price = 10, | ||
PublishDate = DateTime.Now, | ||
Type = BookType.ScienceFiction | ||
} | ||
); | ||
|
||
//Assert | ||
result.Id.ShouldNotBe(Guid.Empty); | ||
result.Name.ShouldBe("New test book 42"); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_Not_Create_A_Book_Without_Name() | ||
{ | ||
var exception = await Assert.ThrowsAsync<AbpValidationException>(async () => | ||
{ | ||
await _bookAppService.CreateAsync( | ||
new CreateUpdateBookDto | ||
{ | ||
Name = "", | ||
Price = 10, | ||
PublishDate = DateTime.Now, | ||
Type = BookType.ScienceFiction | ||
} | ||
); | ||
}); | ||
|
||
exception.ValidationErrors | ||
.ShouldContain(err => err.MemberNames.Any(mem => mem == "Name")); | ||
} | ||
} | ||
} |
43 changes: 38 additions & 5 deletions
43
...ular-MongoDb/aspnet-core/test/Acme.BookStore.TestBase/BookStoreTestDataSeedContributor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,49 @@ | ||
using System.Threading.Tasks; | ||
using System; | ||
using System.Threading.Tasks; | ||
using Volo.Abp.Data; | ||
using Volo.Abp.DependencyInjection; | ||
using Volo.Abp.Domain.Repositories; | ||
using Volo.Abp.Guids; | ||
|
||
namespace Acme.BookStore | ||
{ | ||
public class BookStoreTestDataSeedContributor : IDataSeedContributor, ITransientDependency | ||
public class BookStoreTestDataSeedContributor | ||
: IDataSeedContributor, ITransientDependency | ||
{ | ||
public Task SeedAsync(DataSeedContext context) | ||
private readonly IRepository<Book, Guid> _bookRepository; | ||
private readonly IGuidGenerator _guidGenerator; | ||
|
||
public BookStoreTestDataSeedContributor( | ||
IRepository<Book, Guid> bookRepository, | ||
IGuidGenerator guidGenerator) | ||
{ | ||
_bookRepository = bookRepository; | ||
_guidGenerator = guidGenerator; | ||
} | ||
|
||
public async Task SeedAsync(DataSeedContext context) | ||
{ | ||
/* Seed additional test data... */ | ||
await _bookRepository.InsertAsync( | ||
new Book | ||
{ | ||
Id = _guidGenerator.Create(), | ||
Name = "Test book 1", | ||
Type = BookType.Fantastic, | ||
PublishDate = new DateTime(2015, 05, 24), | ||
Price = 21 | ||
} | ||
); | ||
|
||
return Task.CompletedTask; | ||
await _bookRepository.InsertAsync( | ||
new Book | ||
{ | ||
Id = _guidGenerator.Create(), | ||
Name = "Test book 2", | ||
Type = BookType.Science, | ||
PublishDate = new DateTime(2014, 02, 11), | ||
Price = 15 | ||
} | ||
); | ||
} | ||
} | ||
} |