Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call a method in derived class instead of the parent class #402

Open
amunim opened this issue Jan 19, 2022 · 0 comments
Open

Call a method in derived class instead of the parent class #402

amunim opened this issue Jan 19, 2022 · 0 comments
Assignees
Labels

Comments

@amunim
Copy link

amunim commented Jan 19, 2022

Describe your issue
I have a CMS system which by default every controller gets static content. Some sites need more additional data hence it derives from the BaseApiController and overrides the default behavior so as to include relevant data.
The way it works is that using ControllerContext.ActionDescriptor.ControllerName, we can get the name of the page controller we are at, this works when when the class overrides the default method. But in method where it is not overridden, rather inherited. The value is not up to date only in tests.

Expected behavior
ControllerContext.ActionDescriptor.ControllerName to have derived controller name.

Now, the code

[ApiController]
[Route("[controller]")]
public class BaseApiController : ControllerBase
{
    public virtual async Task<ActionResult<MainLayout>> Index([FromBody]Session session = null)
    {
        return HandleResult(await GetContent<MainLayout>(IDMembership: session?.IDMembership));
    }
}

public class AboutUsController: BaseApiController //not working in tests, but works in browser
{

}

public class ReviewsController: BaseApiController //works in tests, and in the browser
{
   [HttpGet()]
   public override async Task<ActionResult<MainLayout>> Index([FromBody]Session session = null)
   {
       return new ReviewsDTO; //some DTO derived from MainLayout
   }
}
Tests
[Collection("Test")]
public class StaticPages
{
    [Fact]
    public void About() //but will work for Reviews
    {
        MyController<AboutUsController>
        .Instance()
        .Calling(x => x.Index(null))
        .ShouldReturn()
        .Ok(x =>
        {
            x.ShouldPassForThe<MainLayout>(x => x.ParagraphTop.Contains("<h1>About Us</h1>"));
        });
    }
}

Environment:

  • OS: Windows
  • ASP.Net 5 MVC
  • Include="Microsoft.EntityFrameworkCore" Version="5.0.11"
  • net5.0
  • Include="MyTested.AspNetCore.Mvc" Version="5.0.0"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants