Browse Source

update cn_s version tutorial

pull/18700/head
MorosM 3 years ago
parent
commit
ca29b8837f
  1. 114
      docs/zh-Hans/Tutorials/Part-10.md
  2. 76
      docs/zh-Hans/Tutorials/Part-4.md
  3. 139
      docs/zh-Hans/Tutorials/Part-8.md

114
docs/zh-Hans/Tutorials/Part-10.md

@ -622,79 +622,79 @@ using System.Threading.Tasks;
using Acme.BookStore.Authors; using Acme.BookStore.Authors;
using Shouldly; using Shouldly;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.Modularity;
using Volo.Abp.Validation; using Volo.Abp.Validation;
using Xunit; using Xunit;
namespace Acme.BookStore.Books namespace Acme.BookStore.Books;
{ {{if DB=="Mongo"}}
[Collection(BookStoreTestConsts.CollectionDefinitionName)]{{end}} public abstract class BookAppService_Tests<TStartupModule> : BookStoreApplicationTestBase<TStartupModule>
public class BookAppService_Tests : BookStoreApplicationTestBase where TStartupModule : IAbpModule
{
private readonly IBookAppService _bookAppService;
private readonly IAuthorAppService _authorAppService;
protected BookAppService_Tests()
{ {
private readonly IBookAppService _bookAppService; _bookAppService = GetRequiredService<IBookAppService>();
private readonly IAuthorAppService _authorAppService; _authorAppService = GetRequiredService<IAuthorAppService>();
}
public BookAppService_Tests() [Fact]
{ public async Task Should_Get_List_Of_Books()
_bookAppService = GetRequiredService<IBookAppService>(); {
_authorAppService = GetRequiredService<IAuthorAppService>(); //Act
} var result = await _bookAppService.GetListAsync(
new PagedAndSortedResultRequestDto()
);
//Assert
result.TotalCount.ShouldBeGreaterThan(0);
result.Items.ShouldContain(b => b.Name == "1984" &&
b.AuthorName == "George Orwell");
}
[Fact] [Fact]
public async Task Should_Get_List_Of_Books() public async Task Should_Create_A_Valid_Book()
{ {
//Act var authors = await _authorAppService.GetListAsync(new GetAuthorListDto());
var result = await _bookAppService.GetListAsync( var firstAuthor = authors.Items.First();
new PagedAndSortedResultRequestDto()
);
//Assert //Act
result.TotalCount.ShouldBeGreaterThan(0); var result = await _bookAppService.CreateAsync(
result.Items.ShouldContain(b => b.Name == "1984" && new CreateUpdateBookDto
b.AuthorName == "George Orwell"); {
} AuthorId = firstAuthor.Id,
Name = "New test book 42",
Price = 10,
PublishDate = System.DateTime.Now,
Type = BookType.ScienceFiction
}
);
[Fact] //Assert
public async Task Should_Create_A_Valid_Book() result.Id.ShouldNotBe(Guid.Empty);
{ result.Name.ShouldBe("New test book 42");
var authors = await _authorAppService.GetListAsync(new GetAuthorListDto()); }
var firstAuthor = authors.Items.First();
//Act [Fact]
var result = await _bookAppService.CreateAsync( public async Task Should_Not_Create_A_Book_Without_Name()
{
var exception = await Assert.ThrowsAsync<AbpValidationException>(async () =>
{
await _bookAppService.CreateAsync(
new CreateUpdateBookDto new CreateUpdateBookDto
{ {
AuthorId = firstAuthor.Id, Name = "",
Name = "New test book 42",
Price = 10, Price = 10,
PublishDate = System.DateTime.Now, PublishDate = DateTime.Now,
Type = BookType.ScienceFiction Type = BookType.ScienceFiction
} }
); );
});
//Assert exception.ValidationErrors
result.Id.ShouldNotBe(Guid.Empty); .ShouldContain(err => err.MemberNames.Any(m => m == "Name"));
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(m => m == "Name"));
}
} }
} }
``` ```

76
docs/zh-Hans/Tutorials/Part-4.md

@ -85,36 +85,70 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Shouldly; using Shouldly;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.Modularity;
using Volo.Abp.Validation; using Volo.Abp.Validation;
using Xunit; using Xunit;
namespace Acme.BookStore.Books namespace Acme.BookStore.Books;
{ {{if DB=="Mongo"}}
[Collection(BookStoreTestConsts.CollectionDefinitionName)]{{end}}
public class BookAppService_Tests : BookStoreApplicationTestBase
{
private readonly IBookAppService _bookAppService;
public BookAppService_Tests() public abstract class BookAppService_Tests<TStartupModule> : BookStoreApplicationTestBase<TStartupModule>
{ where TStartupModule : IAbpModule
_bookAppService = GetRequiredService<IBookAppService>(); {
} private readonly IBookAppService _bookAppService;
[Fact] protected BookAppService_Tests()
public async Task Should_Get_List_Of_Books() {
{ _bookAppService = GetRequiredService<IBookAppService>();
//Act }
var result = await _bookAppService.GetListAsync(
new PagedAndSortedResultRequestDto()
);
//Assert [Fact]
result.TotalCount.ShouldBeGreaterThan(0); public async Task Should_Get_List_Of_Books()
result.Items.ShouldContain(b => b.Name == "1984"); {
} //Act
var result = await _bookAppService.GetListAsync(
new PagedAndSortedResultRequestDto()
);
//Assert
result.TotalCount.ShouldBeGreaterThan(0);
result.Items.ShouldContain(b => b.Name == "1984");
} }
} }
```` ````
{{if DB == "EF"}}
添加 `BookAppService_Tests` 的实现类,命名为 `EfCoreBookAppService_Tests` ,并放置在 `EntityFrameworkCore\Applications\Books` 命名空间 (文件夹)下,在 `Acme.BookStore.EntityFrameworkCore.Tests` 项目中:
````csharp
using Acme.BookStore.Books;
using Xunit;
namespace Acme.BookStore.EntityFrameworkCore.Applications.Books;
[Collection(BookStoreTestConsts.CollectionDefinitionName)]
public class EfCoreBookAppService_Tests : BookAppService_Tests<BookStoreEntityFrameworkCoreTestModule>
{
}
````
{{end}}
{{if DB == "Mongo"}}
添加 `BookAppService_Tests` 的实现类,命名为`MongoDBBookAppService_Tests` ,并放置在 `MongoDb\Applications\Books` 命名空间 (文件夹)下,在 `Acme.BookStore.MongoDB.Tests` 项目中:
````csharp
using Acme.BookStore.MongoDB;
using Acme.BookStore.Books;
using Xunit;
namespace Acme.BookStore.MongoDb.Applications.Books;
[Collection(BookStoreTestConsts.CollectionDefinitionName)]
public class MongoDBBookAppService_Tests : BookAppService_Tests<BookStoreMongoDbTestModule>
{
}
````
{{end}}
* 测试方法 `Should_Get_List_Of_Books` 直接使用 `BookAppService.GetListAsync` 方法来获取用户列表,并执行检查. * 测试方法 `Should_Get_List_Of_Books` 直接使用 `BookAppService.GetListAsync` 方法来获取用户列表,并执行检查.
* 我们可以安全地检查 "1984" 这本书的名称,因为我们知道这本书可以在数据库中找到,我们已将其添加到种子数据中. * 我们可以安全地检查 "1984" 这本书的名称,因为我们知道这本书可以在数据库中找到,我们已将其添加到种子数据中.

139
docs/zh-Hans/Tutorials/Part-8.md

@ -497,77 +497,112 @@ namespace Acme.BookStore
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Shouldly; using Shouldly;
using Volo.Abp.Modularity;
using Xunit; using Xunit;
namespace Acme.BookStore.Authors namespace Acme.BookStore.Authors;
{ {{if DB=="Mongo"}}
[Collection(BookStoreTestConsts.CollectionDefinitionName)]{{end}} public abstract class AuthorAppService_Tests<TStartupModule> : BookStoreApplicationTestBase<TStartupModule>
public class AuthorAppService_Tests : BookStoreApplicationTestBase where TStartupModule : IAbpModule
{
private readonly IAuthorAppService _authorAppService;
protected AuthorAppService_Tests()
{ {
private readonly IAuthorAppService _authorAppService; _authorAppService = GetRequiredService<IAuthorAppService>();
}
public AuthorAppService_Tests() [Fact]
{ public async Task Should_Get_All_Authors_Without_Any_Filter()
_authorAppService = GetRequiredService<IAuthorAppService>(); {
} var result = await _authorAppService.GetListAsync(new GetAuthorListDto());
[Fact] result.TotalCount.ShouldBeGreaterThanOrEqualTo(2);
public async Task Should_Get_All_Authors_Without_Any_Filter() result.Items.ShouldContain(author => author.Name == "George Orwell");
{ result.Items.ShouldContain(author => author.Name == "Douglas Adams");
var result = await _authorAppService.GetListAsync(new GetAuthorListDto()); }
result.TotalCount.ShouldBeGreaterThanOrEqualTo(2); [Fact]
result.Items.ShouldContain(author => author.Name == "George Orwell"); public async Task Should_Get_Filtered_Authors()
result.Items.ShouldContain(author => author.Name == "Douglas Adams"); {
} var result = await _authorAppService.GetListAsync(
new GetAuthorListDto {Filter = "George"});
[Fact] result.TotalCount.ShouldBeGreaterThanOrEqualTo(1);
public async Task Should_Get_Filtered_Authors() result.Items.ShouldContain(author => author.Name == "George Orwell");
{ result.Items.ShouldNotContain(author => author.Name == "Douglas Adams");
var result = await _authorAppService.GetListAsync( }
new GetAuthorListDto {Filter = "George"});
result.TotalCount.ShouldBeGreaterThanOrEqualTo(1); [Fact]
result.Items.ShouldContain(author => author.Name == "George Orwell"); public async Task Should_Create_A_New_Author()
result.Items.ShouldNotContain(author => author.Name == "Douglas Adams"); {
} var authorDto = await _authorAppService.CreateAsync(
new CreateAuthorDto
{
Name = "Edward Bellamy",
BirthDate = new DateTime(1850, 05, 22),
ShortBio = "Edward Bellamy was an American author..."
}
);
authorDto.Id.ShouldNotBe(Guid.Empty);
authorDto.Name.ShouldBe("Edward Bellamy");
}
[Fact] [Fact]
public async Task Should_Create_A_New_Author() public async Task Should_Not_Allow_To_Create_Duplicate_Author()
{
await Assert.ThrowsAsync<AuthorAlreadyExistsException>(async () =>
{ {
var authorDto = await _authorAppService.CreateAsync( await _authorAppService.CreateAsync(
new CreateAuthorDto new CreateAuthorDto
{ {
Name = "Edward Bellamy", Name = "Douglas Adams",
BirthDate = new DateTime(1850, 05, 22), BirthDate = DateTime.Now,
ShortBio = "Edward Bellamy was an American author..." ShortBio = "..."
} }
); );
});
}
authorDto.Id.ShouldNotBe(Guid.Empty); //TODO: Test other methods...
authorDto.Name.ShouldBe("Edward Bellamy"); }
} ````
[Fact] {{if DB == "EF"}}
public async Task Should_Not_Allow_To_Create_Duplicate_Author() 添加 `AuthorAppService_Tests` 的实现类,命名为 `EfCoreAuthorAppService_Tests` ,并放置在 `EntityFrameworkCore\Applications\Authors` 命名空间 (文件夹)下,在 `Acme.BookStore.EntityFrameworkCore.Tests` 项目中:
{
await Assert.ThrowsAsync<AuthorAlreadyExistsException>(async () => ````csharp
{ using Acme.BookStore.Authors;
await _authorAppService.CreateAsync( using Xunit;
new CreateAuthorDto
{ namespace Acme.BookStore.EntityFrameworkCore.Applications.Authors;
Name = "Douglas Adams",
BirthDate = DateTime.Now, [Collection(BookStoreTestConsts.CollectionDefinitionName)]
ShortBio = "..." public class EfCoreAuthorAppService_Tests : AuthorAppService_Tests<BookStoreEntityFrameworkCoreTestModule>
} {
);
});
}
//TODO: Test other methods...
}
} }
```` ````
{{end}}
{{if DB == "Mongo"}}
添加 `AuthorAppService_Tests` 的实现类,命名为 `MongoDBAuthorAppService_Tests` ,并放置在 `MongoDb\Applications\Authors` 命名空间 (文件夹)下,在 `Acme.BookStore.MongoDB.Tests` 项目中:
````csharp
using Acme.BookStore.MongoDB;
using Acme.BookStore.Authors;
using Xunit;
namespace Acme.BookStore.MongoDb.Applications.Authors;
[Collection(BookStoreTestConsts.CollectionDefinitionName)]
public class MongoDBAuthorAppService_Tests : AuthorAppService_Tests<BookStoreMongoDbTestModule>
{
}
````
{{end}}
完成应用服务方法的测试, 它们应该很容易理解. 完成应用服务方法的测试, 它们应该很容易理解.

Loading…
Cancel
Save