Browse Source

Created unit test for Crud application service.

pull/96/head
Halil İbrahim Kalkan 9 years ago
parent
commit
c52704fdaa
  1. 8
      src/Volo.Abp/Volo/Abp/AbpServiceBase.cs
  2. 13
      src/Volo.Abp/Volo/Abp/Application/Services/CrudAppServiceBase.cs
  3. 2
      test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/AppModule.cs
  4. 111
      test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/PersonAppService_Tests.cs
  5. 5
      test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Person.cs
  6. 20
      test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs

8
src/Volo.Abp/Volo/Abp/AbpServiceBase.cs

@ -1,6 +1,7 @@
using System;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.Guids;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Uow;
@ -12,6 +13,8 @@ namespace Volo.Abp
public IObjectMapper ObjectMapper { get; set; }
public IGuidGenerator GuidGenerator { get; set; }
protected IUnitOfWork CurrentUnitOfWork => UnitOfWorkManager?.Current;
public ILoggerFactory LoggerFactory { get; set; }
@ -23,5 +26,10 @@ namespace Volo.Abp
- Setting manager
- Localization manager and helper methods
*/
protected AbpServiceBase()
{
GuidGenerator = SimpleGuidGenerator.Instance;
}
}
}

13
src/Volo.Abp/Volo/Abp/Application/Services/CrudAppServiceBase.cs

@ -18,7 +18,7 @@ namespace Volo.Abp.Application.Services
where TUpdateInput : IEntityDto<TPrimaryKey>
{
protected IQueryableRepository<TEntity, TPrimaryKey> Repository { get; }
protected virtual string GetPermissionName { get; set; }
protected virtual string GetAllPermissionName { get; set; }
@ -115,7 +115,16 @@ namespace Volo.Abp.Application.Services
/// </summary>
protected virtual TEntity MapToEntity(TCreateInput createInput)
{
return ObjectMapper.Map<TCreateInput, TEntity>(createInput);
var entity = ObjectMapper.Map<TCreateInput, TEntity>(createInput);
//Set ID for GUIDs
var entityWithGuidId = entity as IEntity<Guid>;
if (entityWithGuidId != null && entityWithGuidId.Id == Guid.Empty)
{
entityWithGuidId.Id = GuidGenerator.Create();
}
return entity;
}
/// <summary>

2
test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/AppModule.cs

@ -26,6 +26,8 @@ namespace Volo.Abp.AspNetCore.App
{
options.CreateControllersForAppServices(typeof(TestAppModule).Assembly);
});
services.AddAssemblyOf<AppModule>();
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)

111
test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/PersonAppService_Tests.cs

@ -1,18 +1,127 @@
using Shouldly;
using System;
using Shouldly;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Application.Dtos;
using Volo.Abp.TestApp.Application;
using Volo.Abp.TestApp.Domain;
using Xunit;
using Volo.Abp.Domain.Repositories;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Json;
namespace Volo.Abp.AspNetCore.Mvc
{
//TODO: Refactor to make tests easier.
public class PersonAppService_Tests : AspNetCoreMvcTestBase
{
private readonly IQueryableRepository<Person> _personRepository;
private readonly IJsonSerializer _jsonSerializer;
private readonly IObjectMapper _objectMapper;
public PersonAppService_Tests()
{
_personRepository = ServiceProvider.GetRequiredService<IQueryableRepository<Person>>();
_jsonSerializer = ServiceProvider.GetRequiredService<IJsonSerializer>();
_objectMapper = ServiceProvider.GetRequiredService<IObjectMapper>();
}
[Fact]
public async Task GetAll_Test()
{
//Ideally should be [GET] /api/app/person
var result = await GetResponseAsObjectAsync<ListResultDto<PersonDto>>("/api/services/app/person/GetAll");
result.Items.Count.ShouldBeGreaterThan(0);
}
[Fact]
public async Task Get_Test()
{
var firstPerson = _personRepository.GetList().First();
//Ideally should be [GET] /api/app/person/{id}
var result = await GetResponseAsObjectAsync<PersonDto>("/api/services/app/person/Get?id=" + firstPerson.Id);
result.Name.ShouldBe(firstPerson.Name);
}
[Fact]
public async Task Delete_Test()
{
var firstPerson = _personRepository.GetList().First();
//Ideally should be [DELETE] /api/app/person/{id}
await Client.DeleteAsync("/api/services/app/person/Delete?id=" + firstPerson.Id);
(await _personRepository.FindAsync(firstPerson.Id)).ShouldBeNull();
}
[Fact]
public async Task Create_Test()
{
//Act
var postData = _jsonSerializer.Serialize(new PersonDto {Name = "John", Age = 33});
//Ideally should be [POST] /api/app/person
var response = await Client.PostAsync(
"/api/services/app/person/Create",
new StringContent(postData, Encoding.UTF8, "application/json")
);
response.IsSuccessStatusCode.ShouldBeTrue();
response.StatusCode.ShouldBe(HttpStatusCode.OK);
var resultAsString = await response.Content.ReadAsStringAsync();
PersonDto resultDto = _jsonSerializer.Deserialize<PersonDto>(resultAsString);
//Assert
resultDto.Id.ShouldNotBe(default(Guid));
resultDto.Name.ShouldBe("John");
resultDto.Age.ShouldBe(33);
(await _personRepository.FindAsync(resultDto.Id)).ShouldNotBeNull();
}
[Fact]
public async Task Update_Test()
{
//Arrange
var firstPerson = _personRepository.GetList().First();
var firstPersonAge = firstPerson.Age; //Persist to a variable since we are using in-memory database which shares same entity.
var updateDto = _objectMapper.Map<Person, PersonDto>(firstPerson);
updateDto.Age = updateDto.Age + 1;
var putData = _jsonSerializer.Serialize(updateDto);
//Act
//Ideally should be [PUT] /api/app/person
var response = await Client.PutAsync(
"/api/services/app/person/Update",
new StringContent(putData, Encoding.UTF8, "application/json")
);
response.IsSuccessStatusCode.ShouldBeTrue();
response.StatusCode.ShouldBe(HttpStatusCode.OK);
var resultAsString = await response.Content.ReadAsStringAsync();
PersonDto resultDto = _jsonSerializer.Deserialize<PersonDto>(resultAsString);
//Assert
resultDto.Id.ShouldBe(firstPerson.Id);
resultDto.Name.ShouldBe(firstPerson.Name);
resultDto.Age.ShouldBe(firstPersonAge + 1);
var personInDb = (await _personRepository.FindAsync(resultDto.Id));
personInDb.ShouldNotBeNull();
personInDb.Name.ShouldBe(firstPerson.Name);
personInDb.Age.ShouldBe(firstPersonAge + 1);
}
}
}

5
test/Volo.Abp.TestApp/Volo/Abp/TestApp/Domain/Person.cs

@ -9,6 +9,11 @@ namespace Volo.Abp.TestApp.Domain
public int Age { get; set; }
private Person()
{
}
public Person(Guid id, string name, int age)
{
Id = id;

20
test/Volo.Abp.TestApp/Volo/Abp/TestApp/TestAppModule.cs

@ -11,14 +11,7 @@ namespace Volo.Abp.TestApp
{
public override void ConfigureServices(IServiceCollection services)
{
services.Configure<AbpAutoMapperOptions>(options =>
{
options.Configurators.Add((IAbpAutoMapperConfigurationContext ctx) =>
{
ctx.MapperConfiguration.CreateMap<Person, PersonDto>();
});
});
ConfigureAutoMapper(services);
services.AddAssemblyOf<TestAppModule>();
}
@ -27,6 +20,17 @@ namespace Volo.Abp.TestApp
SeedTestData(context);
}
private static void ConfigureAutoMapper(IServiceCollection services)
{
services.Configure<AbpAutoMapperOptions>(options =>
{
options.Configurators.Add((IAbpAutoMapperConfigurationContext ctx) =>
{
ctx.MapperConfiguration.CreateMap<Person, PersonDto>().ReverseMap();
});
});
}
private static void SeedTestData(ApplicationInitializationContext context)
{
using (IServiceScope scope = context.ServiceProvider.CreateScope())

Loading…
Cancel
Save