TDD - How to write test case for a method that as Assembly.LoadFrom(…)

后端 未结 1 1351
小蘑菇
小蘑菇 2021-01-24 05:44

I have got method which is using Assembly.LoadFrom(...) statement and returns the supported cultures from that satellite assembly, so how could I write unit tests for that type

1条回答
  •  盖世英雄少女心
    2021-01-24 05:54

    Is this the situation?

       aMethod(whatToLoad) {
              // other stuff
    
              x = Assembly.LoadFrom( whatToLoad );
    
              // code using x
      }
    

    First principle: We are focusing on testing aMethod(), the testing of Assembly.LoadFrom() is a separate problem. While we are building tests for aMethod() we don't try to test its dependencies.

    So here what kind of tests might we need?

    1. That we pass the right value for whatToLoad
    2. That we correctly store/use the value returned
    3. That we correctly handle errors or exceptions thrown from Assembly.LoadFrom()

    It is easiest to do this if the test can supply a Mock implementation. Then we can test 1 by checking that the Mock received the expected value. Test 2 by returning a well defined value (or for mulitiple tests different interesting values) Test 3 by generating chosen error conditions.

    So you have changed your code to something like this:

      aMethod(loader, whatToLoad) {
              // other code
    
              x = loader.Load( whatToLoad );
    
              // code using x
      }
    

    Maybe the loaded is injected in some other way, but the point is that you can now specify different tests my setting up a suitable loaoder. For example, for the first test.

    testLoader = new MockLoaderThatRembers();
    
    tested.aMethod(testLoader, loadThis);
    
    assertEquals(testLoader.getLoader(), loadThis);
    

    So if that's the kind of thing you are doing then yes, I'd say you're enabling TDD.

    0 讨论(0)
提交回复
热议问题