在本章中,无涯教程将创建一个新的Ruby类,将其保存在自己的文件中,并创建一个单独的spec文件来测试该类。
首先,在新类中,它称为 StringAnalyzer 。这是一个简单的类,它分析字符串。类只有一个方法 has_vowels?,顾名思义,如果字符串包含元音,则返回true,否则返回false。这是 StringAnalyzer 的代码实现-
class StringAnalyzer def has_vowels?(str) !!(str =~ /[aeio]+/i) end end
如果遵循HelloWorld部分,则会创建一个名为C:\rspec_tutorial\spec的文件夹。
这是测试StringAnalyzer代码-
require 'string_analyzer' describe StringAnalyzer do context "With valid input" do it "should detect when a string contains vowels" do sa = StringAnalyzer.new test_string = 'uuu' expect(sa.has_vowels? test_string).to be true end it "should detect when a string doesn't contain vowels" do sa = StringAnalyzer.new test_string = 'bcdfg' expect(sa.has_vowels? test_string).to be false end end end
将其保存在相同的spec目录中,并命名为string_analyzer_test.rb。
在您的cmd.exe窗口中,cd到C:\rspec_tutorial文件夹并运行以下命令:dir spec ,您应该看到以下内容-
C:\rspec_tutorial\spec 目录
09/13/2015 08:22 AM <DIR> . 09/13/2015 08:22 AM <DIR> .. 09/12/2015 11:44 PM 81 string_analyzer.rb 09/12/2015 11:46 PM 451 string_analyzer_test.rb
现在要运行测试,运行以下命令:rspec spec
当您将文件夹名称传递给 rspec 时,它将运行该文件夹内的所有规范文件。您应该看到此输出-
No examples found. Finished in 0 seconds (files took 0.068 seconds to load) 0 examples, 0 failures
发生这种情况的原因是,默认情况下, rspec 仅运行名称以" _spec.rb"结尾的文件。将string_analyzer_test.rb重命名为string_analyzer_spec.rb。您可以通过运行以下命令轻松做到这一点-
ren spec\string_analyzer_test.rb string_analyzer_spec.rb
现在,再次运行 rspec spec,您应该看到如下所示的输出-
F. Failures: 1) StringAnalyzer With valid input should detect when a string contains vowels Failure/Error: expect(sa.has_vowels? test_string).to be true expected true got false # ./spec/string_analyzer_spec.rb:9:in `block (3 levels) in <top (required)>' Finished in 0.015 seconds (files took 0.12201 seconds to load) 2 examples, 1 failure Failed examples: rspec ./spec/string_analyzer_spec.rb:6 # StringAnalyzer With valid input should detect when a string contains vowels Do you see what just happened? Our spec failed because we have a bug in StringAnalyzer. The bug is simple to fix, open up string_analyzer.rb in a text editor and change this line: !!(str =~ /[aeio]+/i) to this: !!(str =~ /[aeiou]+/i)
现在,将您刚刚所做的更改保存在string_analyizer.rb中,然后再次运行rspec spec命令,您现在应该看到如下输出:
.. Finished in 0.002 seconds (files took 0.11401 seconds to load) 2 examples, 0 failures
恭喜,您的spec文件中的示例现在通过了。修复了具有元音方法的正则表达式中的一个错误,但测试还远远没有完成。
添加更多示例以使用has vowels方法测试各种类型的输入字符串将是有意义的。
rspec 命令提供了许多不同的选项,要查看所有选项,请键入 rspec -help。下表列出了最受欢迎的选项,并说明了它们的作用。
Sr.No. | Option/flag & Remark |
---|---|
1 | -I PATH 将PATH添加到 rspec 在查找Ruby源文件时使用的加载(需要)路径。 |
2 | -r,--require PATH 添加规范中需要的特定源文件。文件。 |
3 | --fail-fast 使用此选项,在第一个示例失败后,rspec将停止运行规范。默认情况下,rspec运行所有指定的spec文件,无论有多少次失败。 链接:https://www.learnfk.comhttps://www.learnfk.com/rspec/rspec-writing-specs.html 来源:LearnFk无涯教程网 |
4 | -f,-format FORMATTER 此选项允许您指定不同的输出格式。有关输出格式的更多详细信息,请参见格式化程序部分。 |
5 | -o,-out FILE 此选项指示rspec将测试输出写入输出文件FILE,而不是标准输出。 |
6 | -c,--color 在rspec的输出中启用颜色。成功的示例输出将以绿色文本显示,失败的输出将以红色文本显示。 |
7 | -b,--backtrace 在rspec的输出中显示完整的错误回溯。 |
8 | -w,-warnings 在rspec的输出中显示Ruby警告。 |
9 | -P,--pattern PATTERN 加载并运行与模式PATTERN相匹配的规格文件。例如,如果传递-p" * .rb",则rspec将运行所有Ruby文件,而不仅仅是以" _spec.rb"结尾的文件。 |
10 | -e,-example STRING 此选项指示rspec运行描述中包含文本STRING的所有示例。 |
11 | -t,--tag DAY 使用此选项,rspec将仅运行包含标签TAG的示例。请注意,TAG被指定为Ruby符号。有关更多详细信息,请参见" RSpec标签"部分。 |
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)