BDD/TDD:依赖可以是一种行为吗?

发布于 2024-08-01 18:27:40 字数 299 浏览 3 评论 0原文

我被告知不要使用实施细节。 依赖关系看起来像是一个实现细节。 但我也可以将其表述为一种行为。

示例:LinkList 依赖于存储引擎来存储其链接(例如 LinkStorageInterface)。 构造函数需要传递一个已实现的 LinkStorageInterface 的实例才能完成其工作。 我不能说“shouldUseLinkStorage”。 但也许我可以说“shouldStoreLinksInStorage”。

在这种情况下,什么是正确的“测试”? 我应该测试它是否在商店中存储链接(行为)还是根本不测试它?

I've been told not to use implementation details. A dependency seems like an implementation detail. However I could phrase it also as a behavior.

Example: A LinkList depends on a storage engine to store its links (eg LinkStorageInterface). The constructor needs to be passed an instance of an implemented LinkStorageInterface to do its job.
I can't say 'shouldUseLinkStorage'. But maybe I can say 'shouldStoreLinksInStorage'.

What is correct to 'test' in this case? Should I test that it stores links in a store (behavior) or don't test this at all?

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

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

发布评论

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

评论(2

划一舟意中人 2024-08-08 18:27:40

LinkStorageInterface 不是一个实现细节——它的名字暗示了一个引擎的接口。 在这种情况下,名称 shouldUseLinkStorageshouldStoreLinksInStorage 更有价值。

这就是我的2便士的价值!

LinkStorageInterface is not an implementation detail - its name suggests an interface to to an engine. In which case the name shouldUseLinkStorage has more value than shouldStoreLinksInStorage.

That's my 2 pennies worth!

岁月静好 2024-08-08 18:27:40

依赖关系本身不是预期的行为,但对依赖关系调用的操作肯定是预期的行为。 您应该测试您(调用者)了解的内容,并避免测试需要您深入了解 SUT 内部工作原理的内容。

稍微扩展一下您的示例,让我们想象一下我们的 LinkStorageInterface 具有以下定义(伪代码):

Interface LinkStorageInterface

  void WriteListToPersistentMedium(LinkList list)

End Interface

现在,由于您(调用者)正在提供该接口的具体实现,因此您测试 WriteListToPercientMedium 是完全合理的当您在 LinkList 上调用 Save() 方法时,() 就会被调用。

测试可能看起来像这样,再次使用伪代码:

void ShouldSaveLinkListToPersistentMedium()

  define da = new MockLinkListStorage()  
  define list = new LinkList(da)

  list.Save()

  Assert.Method(da.WriteListToPersistentMedium).WasCalledWith(list)

end method

您已经测试了预期的行为,而没有测试 SUT 或模拟的实现特定细节。 您想要避免的测试(大多数)是这样的:

  1. 调用方法的顺序 使
  2. 方法或属性公开,以便您可以检查它
  3. 任何不直接涉及您正在测试的预期行为的东西

再次强调,依赖关系是一种东西您作为该类的使用者所提供的,因此您希望它被使用。 否则,一开始就有这种依赖是没有意义的。

The dependency itself is not an expected behavior, but the actions called on the dependency most certainly are. You should test the stuff you (the caller) know about, and avoid testing the stuff that requires you to have intimate knowledge of the inner workings of the SUT.

Expanding your example a little, lets imagine that our LinkStorageInterface has the following definition (Pseudo-Code):

Interface LinkStorageInterface

  void WriteListToPersistentMedium(LinkList list)

End Interface

Now, since you (the caller) are providing the concrete implementation for that interface it is perfectly reasonable for you to test that WriteListToPersistentMedium() gets called when you invoke the Save() method on your LinkList.

A test might look like this, again using pseudo-code:

void ShouldSaveLinkListToPersistentMedium()

  define da = new MockLinkListStorage()  
  define list = new LinkList(da)

  list.Save()

  Assert.Method(da.WriteListToPersistentMedium).WasCalledWith(list)

end method

You have tested expected behavior without testing implementation specific details of either your SUT, or your mock. What you want to avoid testing (mostly) are things like:

  1. Order in which methods were called
  2. Making a method, or property public just so you can check it
  3. Anything that does not directly involve the expected behavior you are testing

Again, a dependency is something that you as the consumer of the class are providing, so you expect it to be used. Otherwise there is no point in having that dependency in the first place.

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