- RSpec 教程
- RSpec - 主页
- RSpec - 简介
- RSpec - 基本语法
- RSpec - 编写规范
- RSpec - 匹配器
- RSpec - 测试替身
- RSpec - 存根
- RSpec - 钩子
- RSpec - 标记
- RSpec - 主体
- RSpec - 帮助器
- RSpec - 元数据
- RSpec - 筛选
- RSpec - 预期
- RSpec 资源
- RSpec - 快速指南
- RSpec - 有用资源
- RSpec - 讨论
RSpec - 标记
RSpec 标记提供了一种简单的方法来运行规范文件中的特定测试。默认情况下,RSpec 会运行它运行的所有规范文件中全部测试,但你可能只需要运行其中一个子集。让我们说你有一些运行非常快速的测试,并且你刚刚对应用程序代码做出了更改,只想运行快速测试,此代码将演示如何使用 RSpec 标记来实现这一点。
describe "How to run specific Examples with Tags" do
it 'is a slow test', :slow = > true do
sleep 10
puts 'This test is slow!'
end
it 'is a fast test', :fast = > true do
puts 'This test is fast!'
end
end
现在,将上述代码保存在名为 tag_spec.rb 的新文件中。从命令行,运行以下命令:rspec --tag slow tag_spec.rb
你将看到以下输出 −
运行选项:包含 {: slow = >true}
This test is slow! . Finished in 10 seconds (files took 0.11601 seconds to load) 1 example, 0 failures
然后,运行以下命令:rspec --tag fast tag_spec.rb
你将看到以下输出 −
Run options: include {:fast = >true}
This test is fast!
.
Finished in 0.001 seconds (files took 0.11201 seconds to load)
1 example, 0 failures
如你所见,RSpec 标记使运行测试子集变得非常容易!
广告