TDD - 如何为作为 Assembly.LoadFrom(...) 的方法编写测试用例 [英] TDD - How to write test case for a method that as Assembly.LoadFrom(...)

查看:50
本文介绍了TDD - 如何为作为 Assembly.LoadFrom(...) 的方法编写测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用 Assembly.LoadFrom(...) 语句的方法,并从该附属程序集返回支持的文化,那么我如何为该类型的方法编写单元测试.

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?

推荐答案

是这种情况吗?

   aMethod(whatToLoad) {
          // other stuff

          x = Assembly.LoadFrom( whatToLoad );

          // code using x
  }

第一原则:我们专注于测试aMethod(),对Assembly.LoadFrom()的测试是一个单独的问题.当我们为 aMethod() 构建测试时,我们不会尝试测试它的依赖项.

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. 我们为 whatToLoad 传递了正确的值
  2. 我们正确存储/使用返回的值
  3. 我们正确处理了从 Assembly.LoadFrom() 抛出的错误或异常

如果测试可以提供 Mock 实现,则最容易做到这一点.然后我们可以通过检查 Mock 是否收到预期值来测试 1.测试 2 通过返回一个明确定义的值(或多个测试不同的有趣值)测试 3 通过生成选定的错误条件.

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);

所以,如果这是您正在做的事情,那么是的,我会说您正在启用 TDD.

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

这篇关于TDD - 如何为作为 Assembly.LoadFrom(...) 的方法编写测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆