0

A method in the service which I'm unit testing is calling a static method that is present in another service.

I'm new to unit testing and have no idea how to mock these kinda dependencies. Please suggest!

user526
  • 3
  • 3
  • Well what does the static method do? If it's something you need to replace for tests, then that sounds like it *should* be a dependency with an instance method. (There are tools available that let you mock static method calls, but personally I view the need to do that as a design smell.) – Jon Skeet Dec 09 '15 at 06:49
  • A stored proc gets called in the static method and it returns a list – user526 Dec 09 '15 at 06:52
  • What is the problem that you are facing with these static methods in unit test. Ideally the problem that happens is that some variables retain values when testing static methods which you can easily get rid of by calling the Test CleanUp method in MS unit testing – Megha Dec 09 '15 at 06:54
  • 1
    Any time you've got a static method making a database call, that sounds like there's all kinds of stuff that I'd have injected as a dependency... I'd have some sort of data access service. You could then have your main implementation call the stored proc, and fake implementations for test (or use mocking; I find fakes typically simpler to work with, but that's a different debate). – Jon Skeet Dec 09 '15 at 07:01
  • Probably worth mentioning if you do not have access to the service source code, adding a wrappers around the static class can help with this type of problem – swestner Dec 09 '15 at 07:29

1 Answers1

2

There is already a thread here about this topic. In fact it is not possible to create a service facade (Mock) for static methods. My suggestion here is to refactor your code in that way that you make your class non static and create an interface for it. Than you can inject your dependency class in the normal system via IOC and in the unit test you can create a mock with frameworks like Moq or Rhinomocks.

Community
  • 1
  • 1
Kris
  • 956
  • 1
  • 12
  • 23