Error with Tests
Hello! My ABP Framework version is 8.0.1 and I'm using MVC with Razor Pages for my UI and MySQL for my database
I'm following the guide to make services and I make the GetListAsync service following every step from the book and it works fine! but the problem is when I make a test for it, I get this error:
Error:
ProductManagement.Products.ProductAppService_Tests`1.Should_Get_Product_List
Source: ProductAppService_Tests.cs line 21 //This is the line of the method public async Task Should_Get_Products_List()
Duration: 1 ms
Message:
System.ArgumentException : Cannot create an instance of ProductManagement.Products.ProductAppService_Tests`1[TStartupModule] because Type.ContainsGenericParameters is true.
Stack Trace:
RuntimeType.CreateInstanceCheckThis()
RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
Activator.CreateInstance(Type type, Object[] args)
<>c__DisplayClass0_0.<CreateTestClass>b__0() line 42
ReflectionAbstractionExtensions.CreateTestClass(ITest test, Type testClassType, Object[] constructorArguments, IMessageBus messageBus, ExecutionTimer timer, CancellationTokenSource cancellationTokenSource) line 42
My Tests class: ProductAppService_Tests looks like this:
using Shouldly;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Modularity;
using Xunit;
namespace ProductManagement.Products
{
public class ProductAppService_Tests<TStartupModule>: ProductManagementApplicationTestBase<TStartupModule>
where TStartupModule: IAbpModule
/* The original configuration is without TStartupModule and without the where state, but that gives me an error:
CS0305*/
{
private readonly IProductAppService _productAppService;
public ProductAppService_Tests()
{
_productAppService = GetRequiredService<IProductAppService>();
}
[Fact]
public async Task Should_Get_Product_List()
{
//Act
var output = await _productAppService.GetListAsync(
new PagedAndSortedResultRequestDto()
);
//Assert
output.TotalCount.ShouldBe(3);
output.Items.ShouldContain(x => x.Name == "Acme Monochrome Laser Printer, Compact All-In One");
}
}
}
### Implementation
My method GetListAsync from ProductAppService:
public async Task<PagedResultDto<ProductDto>> GetListAsync(PagedAndSortedResultRequestDto input)
{
var queryable = await _productRepository.WithDetailsAsync(x => x.Category);
queryable = queryable
.Skip(input.SkipCount)
.Take(input.MaxResultCount)
.OrderBy(input.Sorting ?? nameof(Product.Name));
var products = await AsyncExecuter.ToListAsync(queryable);
var count = await _productRepository.GetCountAsync();
return new PagedResultDto<ProductDto>(count, ObjectMapper.Map<List<Product>, List<ProductDto>>(products));
}
}
hi
https://docs.abp.io/en/abp/latest/Tutorials/Part-4?UI=NG&DB=EF
Dear @maliming, I hope this comment finds you well!
I tried to replicate this issue, using the BookStore in the tutorial "https://docs.abp.io/en/abp/latest/Tutorials/Part-4?UI=NG&DB=EF" , and I get the same error as @shinAC01
ABP Framework version is 8.0.5, UI:NG, DB:EF with Postgres as DBMS
System.ArgumentException : Cannot create an instance of Acme.BookStore.Books.BookAppService_Tests`1[TStartupModule] because Type.ContainsGenericParameters is true.
Should_Get_List_Of_Books implementation
When I try to set this class "BookAppService_Tests" as abstract and protected for its constructor as mentioned in the tutorial, and I build the project, I'm no longer able to see the whole project Acme.BookStore.Application.Tests in Test explorer, as shown below
Acme.BookStore.Application.Tests is no longer available
With dotnet test in CMD it gives No test is available
But when I set it back to public class BookAppService_Tests<TStartupModule>... and public BookAppService_Tests()... for the constructor, I'm again able to see the Acme.BookStore.Application.Tests in Test explorer and the Run All Tests gives the exception described above.
Find below the full exception
Best regards!
hi
https://github.com/abpframework/abp-samples/blob/master/BookStore-Mvc-EfCore/test/Acme.BookStore.Application.Tests/Books/BookAppService_Tests.cs#L13
https://github.com/abpframework/abp-samples/blob/master/BookStore-Mvc-EfCore/test/Acme.BookStore.EntityFrameworkCore.Tests/EntityFrameworkCore/Applications/Books/EfCoreBookAppService_Tests.cs
Hi @maliming ,
Thanks for your prompt reply!
Yes I followed all the steps in the tutorial and it worked, however as mentioned in my previous comment, Acme.BookStore.Application.Tests is no longer visible in the Test Explorer, the test should be ran in Acme.BookStore.EntityFrameworkCore.Tests project, EfCoreBookAppService_Tests class instead.
PS: EfCoreBookAppService_Tests is created in EntityFrameworkCore\Applications\Books namespace (folder) of the Acme.BookStore.EntityFrameworkCore.Tests project
Maybe this part in the tutorial could be updated accordingly, people would be tempted to look at BookAppService_Tests to run their tests, while the class is not visible in Test Explorer
Regards!
Thanks. I will update the images in the docs.
https://github.com/abpframework/abp/pull/19384