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

隐身守侯 提交于 2019-12-02 11:53:36

问题


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 of methods.

What I did was, wrapped that static method/logic to return cultures in anther class and used it's instance method. Is this the right approach?


回答1:


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.



来源:https://stackoverflow.com/questions/1397876/tdd-how-to-write-test-case-for-a-method-that-as-assembly-loadfrom

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!