Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing static classes and methods in C# .NET

I'm relatively new to unit testing, and very new to C#, but I've been trying to test code that uses static classes with static methods, and it seems like I have to write huge amounts of boilerplate code in order to test, and that code would then also probably need to be tested.

For example: I'm using the System.Web.Security.Membership class, with a method ValidateUser on it. It seems like I need to create an interface IMembership containing the method ValidateUser, then create a class MembershipWrapper that implements IMembership, implementing the method ValidateUser and passing the arguments on to the actual Membership class. Then I need to have properties on my class that uses the Membership to reference the wrapper so that I can inject the dependency for a mock object during testing.

So to test 1 line of code that uses Membership, I've had to create an interface, and a class, and add a property and constructor code to my class. This seems wrong, so I must be getting something wrong. How should I be going about this testing? I've had a brief look at some frameworks/libraries that do dependency injection, but they still appear to require lots of boilerplate, or a very deep understanding of what's going on under the hood.

like image 681
danpalmer Avatar asked Jul 05 '13 14:07

danpalmer


People also ask

How do you test a static class method?

There are three ways to test the code that calls static methods: Create a wrapper class and use dependency injection. Use a static Func property. Use the Extract and override call.

Can you test static classes?

Unit testing a static method is no different than unit testing a non-static method. Static methods are not untestable in themselves. A static method that holds no state or doesn't change state can be unit tested. As long as the method and its dependencies are idempotent, the method can be unit tested.

Are there static methods in C?

C # in TeluguA static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.

Can I mock static class in unit test?

If you need to truly mock static methods, you need to use a commercial tool like Microsoft Fakes (part of Visual Studio Enterprise) or Typemock Isolator. Or, you can simply create a new class to wrap the static method calls, which itself can be mocked.


2 Answers

I don't see anything wrong in making your system loosely coupled. I believe you don't complain on creating constructor parameters and passing abstract dependencies to your classes. But instantiating dependencies in place looks so much easier, does it?

Also, as I pointed in comments, you can reuse wrappers later. So, that is not such useless work, as it seems from first glance.

like image 51
Sergey Berezovskiy Avatar answered Oct 06 '22 21:10

Sergey Berezovskiy


You are on the right way, and think you are not testing single line of code, in this case you are writing important test to ensure that your code interacts with membership provider in the right way, this is not simple unit test rather "mock-based" integration test. I think it worth creating all these mocks and have covered by tests this part of application.

And yes, it seems overkill but no other way - either you use some helpers/libraries either wrap third-party static dependencies yourself.

like image 24
sll Avatar answered Oct 06 '22 21:10

sll