没有办法决定是否编写一个您可以不假思索地使用的集成测试。任何实际的应用程序都有细微差别和特殊情况挑战任何规则。 但当然有一种通用方法,您可以采用这种方法,除非您发现它不适合:使用验收测试作为集成测试的方法。
旁注:我所说的“集成测试”是指执行不止一层代码的测试,而不是 Rails 特有的含义,它主要被更好的方法和工具所包含。但如果您确实使用 Rails 集成测试,我的回答也适用。
验收测试捕获重要的用户流程。它也是一个集成测试,从 UI 一直到后端运行应用程序的所有层。行为驱动开发 (BDD) 和类似的方法通过为所有重要的用户流程编写验收测试来推动开发。验收测试编写起来很复杂,运行起来也很慢,因此尝试编写尽可能少的测试,这样仍然定义了所有重要的用户流程。
选择验收测试是一门艺术,但经验法则是,如果两个场景涉及不同的参与者和/或不同的主要系统组件(例如 UI 屏幕),则它们应该有单独的验收测试,否则它们是相同的具有详细信息的场景和一次验收测试就足够了。
验收测试对于与您的利益相关者进行沟通(即记录要求)很有用,因此无论您的其他测试策略如何,您都需要它们。但是通常会发现一整套验收测试就是您需要的所有集成测试。不值得接受测试的详细需求可以在单元测试中表达。
在您的示例中,我可能会为用户创建自己的约会的场景编写一个验收测试,而为父母必须提供帮助的场景编写另一个验收测试,因为它们会完全不同。第一个类似于(使用 Gherkin)
When I visit the new appointment page
And I create a new public appointment
And I visit my calendar
Then I see the public appointment
但第二个会完全不同:
When there is an unrestricted user "Dad"
And there is a restricted user "Billy" supervised by "Dad"
And there is a user "Stalker"
When "Billy" visits the new appointment page
And "Billy" creates a new appointment
And "Billy" visits his calendar
Then "Billy" sees a pending appointment
When "Stalker" visits "Billy"'s calendar
Then "Stalker" does not see an appointment
When "Dad" visits "Billy"'s calendar
And "Dad" approves the pending appointment
Then "Dad" sees an appointment
When "Billy" visits his calendar
Then "Billy" sees an appointment
When "Stalker" visits "Billy"'s calendar
Then "Stalker" does not see an appointment
另一方面,我可能不会为有和没有位置的场景编写两个不同的验收测试(假设位置只是一个可能会或可能不会填充的文本字段)。单元测试可能就足够了。