如何对依赖于另一个类来更改某物状态的类进行单元测试?

发布于 2024-09-07 08:37:35 字数 649 浏览 8 评论 0 原文

我有一个类 A ,其中包含类 B 的实例,并且 A 的函数 foo 调用函数 B 的 >set,更新 B 的状态。下面是一个代码示例(Javascript):

A = function () {
    this.init = function (b) {
        this.b = b
    }

    this.foo = function (val) {
        this.b.set(val)
    }

    this.bar = function () {
        return this.b.get()
    }

}

B = function () {
    this.set = function (val) {
        this.v = val
    }

    this.get = function () {
        return this.v
    }
}

How do I对 foo 函数进行单元测试,同时保持对 A 的测试不依赖于 的实现B(使用模拟和存根等等)?

I have a class A which contains an instance of class B, and function foo of A calls function set of B, which updates the state of B. Here is a code example (in Javascript):

A = function () {
    this.init = function (b) {
        this.b = b
    }

    this.foo = function (val) {
        this.b.set(val)
    }

    this.bar = function () {
        return this.b.get()
    }

}

B = function () {
    this.set = function (val) {
        this.v = val
    }

    this.get = function () {
        return this.v
    }
}

How do I unit-test the foo function, while keeping the test for A non-dependent on the implementation of B (using mocks and stubs and what not)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦旅人picnic 2024-09-14 08:37:35

使用模拟,您可以简单地将 B 的模拟传递给 A,它将检查是否使用适当的值调用了 set。如果您没有模拟框架,则可以在 JavaScript 中简单地创建一个对象:

b = {  
       setCalled: false,
       expectedValue: <expected>
       set: function(val) {
         <check that val == this.expectedValue>
          this.setCalled = true;
       }
     }

在设置 b 的测试中,使用给定的 b 创建一个 A 然后调用 A.foo 并检查 b.setCalled 是否更改为 true。您可以类似地向 b 添加 get 方法来检查 A.bar

在这种情况下,您还应该检查气味Feature Envy——当两个类如此紧密耦合时您应该检查以确保您没有错误地使用某些东西。在您的实际示例中可能没问题,但值得检查一下。

Using mocks, you can simply hand A a mock of B, which will check that set was called with the appropriate value. If you don't have a mock framework, in JavaScript you can simply create an object:

b = {  
       setCalled: false,
       expectedValue: <expected>
       set: function(val) {
         <check that val == this.expectedValue>
          this.setCalled = true;
       }
     }

in the test you setup b, create an A with the given b then call A.foo and check that b.setCalled changed to true. You can similarly add a get method to b to check A.bar.

In this case you also should check the smell Feature Envy -- when two classes are this tightly coupled you should check to make certain you are not using something incorrectly. It may be fine in your real example, but it is worth a check.

喵星人汪星人 2024-09-14 08:37:35

我想出了执行此操作的最佳方法,同时确保 A 的测试不依赖于其实现,即创建一个具有有效 getset,但写入临时变量。

测试 A 的代码示例:

// Mock B
b = new function () {
    this.set = function (val) {
        this.v = val
    }

    this.get = function () {
        return this.v
    }
}

// Create an instance of A with Mock B
a = new A().init(b)

// Test A

// ...

I figured out the best way to do this, while making sure that A's test doesn't depend on its implementation, would be to create a mock B that has a working get and set, but writes to a temporary variable.

Code example to test A:

// Mock B
b = new function () {
    this.set = function (val) {
        this.v = val
    }

    this.get = function () {
        return this.v
    }
}

// Create an instance of A with Mock B
a = new A().init(b)

// Test A

// ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文