我有一些昂贵的测试设置,仅对我的规范中的少数示例是必需的,并且如果有必要,它只需要运行一次。因为它很慢,我试图避免将它放在 before(:each) 块中,但 before(:all) 似乎不适合我的需要。我认为一个复杂的因素是昂贵的部分必须在其他一些常见设置之后运行。(这是一个带有搜索引擎的应用程序的水豚测试。创建一些记录后,我需要索引测试数据库以获得搜索结果。)我的设置是这样的:
feature 'some particular feature' do
before(:each) do
# a bunch of common test setup (creating records that this test will use)
end
describe 'simple example #1' do
# a simple example that doesn't need the expensive setup
end
.
.
.
describe 'simple example #N' do
# a simple example that doesn't need the expensive setup
end
describe 'a more complicated example' do
before(:all) do
# expensive_setup that depends on the records created above
end
it 'does something' do ... end
it 'does something else' do ... end
.
.
.
it 'even does this' do ... end
end
end
Run Code Online (Sandbox Code Playgroud)
问题是,当RSpec的与运行实例的背景下more complicated example,该before(:all)块运行之前的before(:each),它取决于块。到目前为止,我不得不将昂贵的设置放在一个before(:each)块而不是一个before(:all)块中。这意味着必须it为该示例中的每个块运行昂贵的设置。有一个更好的方法吗?
更新: 我没有提到昂贵操作的结果仅取决于数据库。因此,由于每个示例都使用相同的数据库设置,因此可以安全地为每个示例重复使用昂贵操作的结果。此外,结果存在于文件系统中,因此不会在示例之间清除。
我在想要走的路是在文件系统中放置某种标记,表明结果很好,不需要重新计算。
我通过计算常见设置的摘要并将其与昂贵的操作结果一起保存来解决这个问题。在执行昂贵的操作之前,请检查当前摘要是否与磁盘上的摘要匹配。如果是这样,就没有必要这样做。由于所有示例共享通用设置,因此昂贵的操作最多只运行一次。
| 归档时间: |
|
| 查看次数: |
848 次 |
| 最近记录: |