RSpec - 编写规范

在本章中,我们将创建一个新的Ruby类,将其保存在自己的文件中,并创建一个单独的spec文件来测试这个类.

首先,在我们的新类中,它被称为 StringAnalyzer .这是一个简单的类,你猜对了,分析字符串.我们的类只有一个方法 has_vowels?顾名思义,如果一个字符串包含元音则返回true,否则返回false.这是 StringAnalyzer的实现 :

class StringAnalyzer 
   def has_vowels?(str) 
      !!(str =~ /[aeio]+/i) 
   end 
end

如果您按照HelloWorld部分,你创建了一个名为C的文件夹:\ rspec_tutorial \ spec.

删除hello_world.rb文件(如果有的话)并将上面的StringAnalyzer代码保存到一个名为string_analyzer.rb的文件中C:\ rspec_tutorial \ specs文件夹.

以下是测试StringAnalyzer&minus的spec文件的来源;

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

将此保存在同一规格目录中,将其命名为string_analyzer_test.rb.

在cmd.exe窗口中,cd到C:\ rspec_tutorial文件夹并运行以下命令:dir spec

你应该看到以下 :

目录C:\ rspec_tutorial \ specl

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 时,它运行文件夹内的所有spec文件.您应该看到此结果 :

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 规范,您应该会看到类似于此的输出;

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命令,您现在应该看到看起来像&minus的输出;

..
Finished in 0.002 seconds (files took 0.11401 seconds to load)
2 examples, 0 failures

恭喜,您的spec文件中的示例(测试)现在正在通过.我们在正则表达式中修复了一个带有元音方法的错误,但我们的测试还远未完成.

添加更多用于测试各种类型的输入字符串的示例是有意义的方法.

下表显示了可以在新示例中添加的一些排列(它阻止)

输入字符串描述has_vowels的预期结果?
'aaa','eee',' iii','o'只有一个元音而没有其他字母.true
'abcefg''至少有一个元音和一些辅音'true
'mnklp'只有辅音.false
''空字符串(无字母)false
'abcde55345& ??'元音,辅音,数字和标点字符.true
'423432 %%% ^& ;'仅限数字和标点字符.false
'AEIOU'仅大写元音.true
'AeiOuuuA'大写只有.或者更低的元音.true
'AbCdEfghI'大写和小写元音和辅音.true
'BCDFG'仅限大写辅音.false
' '仅限空白字符.false

您可以决定将哪些示例添加到spec文件中.有许多条件需要测试,您需要确定最重要的条件子集并最好地测试您的代码.

rspec 命令提供了许多不同的条件选项,要查看所有选项,请键入 rspec -help.下表列出了最受欢迎的选项,并描述了它们的作用.

Sr.No.Option/flag&说明
1

-I PATH

将PATH添加到 rspec 在查找Ruby源文件时使用的加载(require)路径.

2

-r, --require PATH

添加规范中要求的特定源文件. file(s).

3

 --fail-fast

使用此选项,rspec将在第一个示例失败后停止运行规范.默认情况下,rspec运行所有指定的spec文件,无论有多少次失败.

4

-f, - 格式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匹配的spec文件.例如,如果你传递-p"* .rb",rspec将运行所有Ruby文件,而不仅仅是以"_spec.rb"结尾的文件.

10

-e, -  example STRING

此选项指示rspec在其描述中运行包含文本STRING的所有示例.

11

-t, -  tag TAG

使用此选项,rspec将仅运行包含标记TAG的示例.请注意,TAG被指定为Ruby符号.有关更多详细信息,请参阅RSpec标签部分.