Asp.Net MVC and Sealed Classes

I just created a project from the Asp.Net MVC default template (yes, I am a little late to this game – sorry, I have been living happily with Monorail) and ran across this in the AccountController class:

The FormsAuthentication type is sealed and contains static members, so it is difficult to unit test code that calls its members. The interface and helper class below demonstrate how to create an abstract wrapper around such a type in order to make the AccountController code unit testable.

Ha, after experiencing the pain so many times in so many ways running into these sealed / internal classes when trying to unit test, I find it somewhat satisfying to see MS have to workaround their own issues.

I am just glad that it seems like the paradigm is finally shifting at Microsoft and that maybe, just maybe, we’ll start to see less internal sealed classes and more unit testable frameworks (unit testable from my – the user’s – point of view).

👋 Hi! I’m Chad. I’m a Tech Lead turned founder, but I mostly see myself as a web developer specializing in the .NET Core, React, and Node.js ecosystems. I like to build tools to make developers’ lives easier. I am currently working on a modern job platform for .NET Core and Node.js.


What is this?

1 Comment

  1. John Teague
    John Teague

    Unfortunately, that's probably unrealistic. The reason they seal & internal classes is so they don't have to officially support them.

    From what I've heard, getting a type public in the framework is very difficult.

    But it would be nice if they opened up public interfaces, so we could at least stub them and inject them. I think that is becoming more of reality. We'll see with 4.0 I guess