0

I have a class with several static methods that is being used in a CLR SQL stored procedure method. I ended up writing a wrapper with non-static methods that implements an interface of the methods I want.

I'm doing this so that I can write a unit test. However, the unit test is for a static method that also has another dependency that I can't figure out how to work around.

Note: CLR Stored Procedures need to be static.

public class MyHelperWrapper : IMyHelper
{
    public void DoStuff(List<MyObject> list)
    {
        MyHelper.DoStuff(list); // Static method that sends data to database.
    }
}

public class FakeMyHelperWrapper : IMyHelper
{
    public void DoStuff(List<MyObject> list)
    {
        // don't do anything??
    }
}


public class MyCLRClass
{

    public static void My_Stored_Proc(string a, string b)
    {
        MyPrivateStaticMethod(a, b);
    }


    private static void MyPrivateStaticMethod(string a, string b)
    {
        List<MyObj> list = new List<MyObject>();
        MyObject obj = new MyObject(a, b);
        list.Add(obj);

        MyHelperWrapper.DoStuff(list); // MyWrapper is wrapped around the static methods of the class MyHelper
    }

    private static string Format(string b)
    {
        // ... format ...
        return bFormatted;
    }
}

At the end of the day, I guess I really just need to test that the method creates a new object based on the parameters a and b and then adds it to a list.

My issues:

  1. The method is void so what's the proper way to even test this? Just make sure no errors happen?
  2. If I'm injecting fake classes (such as a fake MyHelperWrapper) then I'm basically bypassing a bunch of the code. This seems bad to me?
  3. If I were to continue this route, how can I inject a fake class for MyObject? This seems kind of bad because then I'm not testing that the object is created how I expect.
  4. Is there a better way? Maybe I have to refactor MyObject to use DI as well but this all seems kind of hacky just to test that an object is added to a list.
user3007447
  • 380
  • 3
  • 15
  • When it comes to testing, you may read `static` as `untestable`. The whole idea of `DI` is to get rid of `static`. – vasily.sib Jul 03 '20 at 05:26
  • Related (but no duplicate): https://stackoverflow.com/questions/52325438/how-to-inject-dependency-to-static-class/52327577#52327577 – Steven Jul 03 '20 at 07:39

1 Answers1

0

The way I resolved this was by injecting the dependency (that was causing the issue) into the constructor of MyObject.

user3007447
  • 380
  • 3
  • 15