Base solution for your next web application
Open Closed

Test API Error in ASP.Net Core #3097


User avatar
0
shimulcse created

Hi, I am using ASP.NET Core & Angular v4.0.0 with .NET Core 1.1 framework

I have an API named CreateCountry as like below

public async Task CreateCountry(CreateCountryInput input)
        {
            var country = input.MapTo<Country>();            
            await _CountryRepository.InsertAsync(country);
        }

Test API for CreateCountry as like below

[Fact]
        public async Task Should_Create_New_Country()
        {
            //Arrange

            //Act
            var input = new CreateCountryInput
            {
                CountryCode = "ARG",
                CountryName = "ARGENTINA"                
            };
            await _CountryAppService.CreateCountry(input);

            //Assert
            var allCountries = _CountryRepository.GetAllListAsync();
            allCountries.ShouldNotBeNull();
            var newlyInsertCountry = _CountryRepository.GetAllList().FirstOrDefault(t => t.CountryCode == "ARG");
            newlyInsertCountry.CountryName.ShouldBe("ARGENTINA");
        }

When checking the Test getting an error as below <span style="color:#BF0000">System.InvalidOperationException : Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance.</span>

Am I missing something to add?

Kindly Suggest.

Thanks Shimul


10 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    In the latest version, if you want to use same app service in unit tests, you need to use ObjectMapper. For example:

    ObjectMapper.Map<Country>(input);
    
  • User Avatar
    0
    shimulcse created

    Using ObjectMapper problem is solved. Thanks.

    Observed that some of your implemented tests API failed. What's are the reason?

  • User Avatar
    0
    alirizaadiyahsi created

    Hi,

    For better help,

    1. Could you share some of the failed test detailed message?
    2. Did you use UseStaticMapper like this: <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero-core/blob/dev/aspnet-core/test/MyCompanyName.AbpZeroTemplate.Tests/AbpZeroTemplateTestModule.cs#L37">https://github.com/aspnetzero/aspnet-ze ... ule.cs#L37</a>
    3. In failed tests, are there objectMapper, too?
  • User Avatar
    0
    shimulcse created

    Hi, Total 14 Test Failed

    I have given some of the tests failed details.

    <span style="color:#FF0000">Test Name: SAPL.SODMS.Tests.Authorization.Roles.RoleAppService_Tests.Should_Get_Roles_For_Host</span>

    Test FullName:	SAPL.SODMS.Tests.Authorization.Roles.RoleAppService_Tests.Should_Get_Roles_For_Host (6ed7f976b875de0e042029be6634ecda6c90887e)
    Test Source:	D:\Workarea\Projects\SAPL\SODMS\SODMSBackend\test\SAPL.SODMS.Tests\Authorization\Roles\RoleAppService_Tests.cs : line 20
    Test Outcome:	Failed
    Test Duration:	0:00:11.785
    
    Result StackTrace:	
    at Shouldly.ShouldlyCoreExtensions.AssertAwesomely[T](T actual, Func`2 specifiedConstraint, Object originalActual, Object originalExpected, Func`1 customMessage, String shouldlyMethod)
       at Shouldly.ShouldBeTestExtensions.ShouldBe[T](T actual, T expected, Func`1 customMessage)
       at Shouldly.ShouldBeTestExtensions.ShouldBe[T](T actual, T expected)
       at SAPL.SODMS.Tests.Authorization.Roles.RoleAppService_Tests.&lt;Should_Get_Roles_For_Host&gt;d__2.MoveNext() in D:\Workarea\Projects\SAPL\SODMS\SODMSBackend\test\SAPL.SODMS.Tests\Authorization\Roles\RoleAppService_Tests.cs:line 27
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    Result Message:	Shouldly.ShouldAssertException : 2
        should be
    1
        but was not
    

    Test Name: SAPL.SODMS.Tests.Authorization.Users.UserAppService_Create_Tests.Should_Create_User_For_Host

    Test FullName:	SAPL.SODMS.Tests.Authorization.Users.UserAppService_Create_Tests.Should_Create_User_For_Host (45c9dd1141158b61edde4f100c61a6981a8f9162)
    Test Source:	D:\Workarea\Projects\SAPL\SODMS\SODMSBackend\test\SAPL.SODMS.Tests\Authorization\Users\UserAppService_Create_Tests.cs : line 18
    Test Outcome:	Failed
    Test Duration:	0:00:06.713
    
    Result StackTrace:	
    at Shouldly.ShouldlyCoreExtensions.AssertAwesomely[T](T actual, Func`2 specifiedConstraint, Object originalActual, Object originalExpected, Func`1 customMessage, String shouldlyMethod)
       at Shouldly.ShouldBeTestExtensions.ShouldBe[T](T actual, T expected, Func`1 customMessage)
       at Shouldly.ShouldBeTestExtensions.ShouldBe[T](T actual, T expected)
       at SAPL.SODMS.Tests.Authorization.Users.UserAppService_Create_Tests.<>c__DisplayClass3_0.<<CreateUserAndTestAsync>b__0>d.MoveNext() in D:\Workarea\Projects\SAPL\SODMS\SODMSBackend\test\SAPL.SODMS.Tests\Authorization\Users\UserAppService_Create_Tests.cs:line 86
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
       at SAPL.SODMS.Tests.AppTestBase.<UsingDbContextAsync>d__8.MoveNext() in D:\Workarea\Projects\SAPL\SODMS\SODMSBackend\test\SAPL.SODMS.Tests\AppTestBase.cs:line 108
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
       at SAPL.SODMS.Tests.Authorization.Users.UserAppService_Create_Tests.<CreateUserAndTestAsync>d__3.MoveNext() in D:\Workarea\Projects\SAPL\SODMS\SODMSBackend\test\SAPL.SODMS.Tests\Authorization\Users\UserAppService_Create_Tests.cs:line 78
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
       at SAPL.SODMS.Tests.Authorization.Users.UserAppService_Create_Tests.<Should_Create_User_For_Host>d__0.MoveNext() in D:\Workarea\Projects\SAPL\SODMS\SODMSBackend\test\SAPL.SODMS.Tests\Authorization\Users\UserAppService_Create_Tests.cs:line 21
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    Result Message:	Shouldly.ShouldAssertException : 1
        should be
    null
        but was not
    

    <span style="color:#FF0000">Test Name: SAPL.SODMS.Tests.Authorization.Users.UserAppService_Link_Tests.Should_Link_User_To_Already_Linked_User</span>

    Test FullName:	SAPL.SODMS.Tests.Authorization.Users.UserAppService_Link_Tests.Should_Link_User_To_Already_Linked_User (b46f80720c662a2e585824bbc3e064d058cd927e)
    Test Source:	D:\Workarea\Projects\SAPL\SODMS\SODMSBackend\test\SAPL.SODMS.Tests\Authorization\Users\UserAppService_Link_Tests.cs : line 74
    Test Outcome:	Failed
    Test Duration:	0:00:04.466
    
    Result StackTrace:	
    at SAPL.SODMS.Authorization.Users.UserLinkAppService.<LinkToUser>d__6.MoveNext() in D:\Workarea\Projects\SAPL\SODMS\SODMSBackend\src\SAPL.SODMS.Application\Authorization\Users\UserLinkAppService.cs:line 51
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Abp.Threading.InternalAsyncHelper.<AwaitTaskWithPostActionAndFinally>d__1.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Abp.Threading.InternalAsyncHelper.<AwaitTaskWithFinally>d__0.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
       at SAPL.SODMS.Tests.Authorization.Users.UserAppService_Link_Tests.<Should_Link_User_To_Already_Linked_User>d__9.MoveNext() in D:\Workarea\Projects\SAPL\SODMS\SODMSBackend\test\SAPL.SODMS.Tests\Authorization\Users\UserAppService_Link_Tests.cs:line 91
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    Result Message:	Abp.UI.UserFriendlyException : Login failed!
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @shimulcse,

    Did you disable multi tenancy in your application ? If so, normally these tests should be skipped because they are only valid in multi tenancy is enabled.

    If your case is different, please explain and we will try to fix it. Also can you write the version of AspNet Zero you are using ?

    Thanks.

  • User Avatar
    0
    shimulcse created

    Hi,

    Yes, I have disabled Multitenancy and I am using ASPNETZERO version v4.0.0 (latest)

    Thanks Shimul

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I have tried and those tests are skipped when you disable multi tenancy. Do you use Visual Studio's test explorer or any other tool ?

  • User Avatar
    0
    shimulcse created

    Hi, I am using Visual Studio TEST EXPLORERS (Version 2017)

    To disable Multitenancy I commented code from below file SAPL.SODMS.Core\SODMSCoreModule.cs

    //Enable this line to create a multi-tenant application.
    //Configuration.MultiTenancy.IsEnabled = SODMSConsts.MultiTenancyEnabled;
    

    Do I need to change anything else to skip those tests?

    Thanks Shimul Dey

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Instead of doing that, can you try to enable that line and set

    SODMSConsts.MultiTenancyEnabled = false;
    

    Thanks.

  • User Avatar
    0
    shimulcse created

    Problem Solved.

    Thanks