您为什么认为 BDD 和集成测试不同?
BDD 只是意味着通过所需的行为来驱动您的设计,通常通过一组验收测试来表达。
这些测试可能是涉及许多 [微] 服务的“集成测试”,也可能是指定单个服务或该服务中单个类的所需行为的测试。理想情况下,将在所有这些级别上进行混合测试。重要的是您指定您想要的行为并使用它来推动开发。
您的系统如何实现在某种程度上是无关紧要的,只要它表现出预期的行为。对于将系统视为黑盒的高级测试,这是正确的,并且越往下走,越接近实际代码,这就越不正确(因为此时您正在有效地测试实现)。
因此,我将专注于新功能的预期行为,并首先为这些验收测试编写规范,然后实施您的服务以实现所需的行为,根据需要以务实的方式添加较低级别的测试,请记住测试级别越低,它们就越容易变得脆弱,并且需要在您更改实现时进行更改。
编辑
根据您的问题编辑。
我不同意 BDD 测试应该只测试业务逻辑。事实上,BDD 测试通常更侧重于测试整个系统,将所有部分集成在一起。话虽如此,BDD 只是一种通过指定所需行为的测试风格,并且可以应用于应用程序的任何级别。您可以通过使用 Gherkin 语法指定行为来测试单个类,我们有时会这样做。我们还使用 Gherkin 指定整个系统的预期行为以及我们服务的预期行为。根据我们所针对的级别,这些测试的格式自然会略有不同。
对于系统测试,我们可能有这样的规范:
Scenario: user can perform action A
Given I am a user with access to some feature A
And feature A is enabled for the user
When I call perform action A with parameters 'Bob' and 'John'
Then A 'BobJohn' is created
And notifications are sent to the current user
对于个别服务,我们可能会有类似的测试
Scenario: create messages are handled correctly
Given the service is set up
When a message arrives to create a 'BobJohn'
Then a new entry is added to the database with the key 'BobJohn'
And an outgoing notification message for 'BobJohn' is created
对于个别类,我们可能会有类似的测试
Scenario: Notifier class should send notifications via all users preferred means
Given the current user wants notification by Twitter
And the current user who wants notification by email
When I send the notification 'BobJohn' to the current user
Then the twitter notifier should be invoked with 'BobJohn'
And the email notifier should be invoked with 'BobJohn'
这些都是 BDD 风格的测试,但它们测试系统的不同方面。