Base solution for your next web application
Open Closed

unit test -> Abp.AbpException : Can not set TenantId to 0... #4327


User avatar
0
BobIngham created

Version 5.0.4, Asp.Net Core & Angular. I have followed the instructions for step-by-step development at <a class="postlink" href="https://aspnetzero.com/Documents/Developing-Step-By-Step-Angular">https://aspnetzero.com/Documents/Develo ... ep-Angular</a>. I have refactored over the past week and documented the procedure to work with database-first procedures and separate components for create/edit/list in accordance with company standards. Having refactored the system works perfectly. However, when I come to run tests with multi-tenancy set to true I am getting the error message "Message: Abp.AbpException : Can not set TenantId to 0 for IMustHaveTenant entities!". My test code:

[Fact]
        public async Task Should_Create_ActionType_With_Valid_Arguments()
        {
            //Act
            await _actionTypeService.CreateActionType(
                new CreateActionTypeInput
                {
                    Name = "Metrics",
                    Description = "A category for measured inputs such as weight, blood pressure, fluid intake etc."
                });

            //Assert
            UsingDbContext(
                context =>
                {
                    var metrics = context.ActionTypes.FirstOrDefault(p => p.Description == "A category for measured inputs such as weight, blood pressure, fluid intake etc.");
                    metrics.ShouldNotBe(null);
                    metrics.Name.ShouldBe("Metrics");
                });
        }

I have read carefully the post on testing ABP at <a class="postlink" href="https://www.codeproject.com/Articles/871786/Unit-testing-in-Csharp-using-xUnit-Entity-Framewor">https://www.codeproject.com/Articles/87 ... y-Framewor</a> and have also debugged the test and stepped through AppTestBase but can still not find why the test is not implementing the default TenantId (1). Help anyone?

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

3 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    TenantId is only set to 1 (Default Tenant) by default in a single-tenant application.

    Should you LoginAsTenant before calling CreateActionType?

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    BobIngham created

    Aaron, great shout. Sorry, I'm a newbie, your directions led me to the right place.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    aaron created
    Support Team

    That's great. As a side note, this forum uses square brackets:

    [code]Code goes here!
    

    [/code:2pjwph53]

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)