📜  RSpec-编写规范

📅  最后修改于: 2020-12-06 10:54:57             🧑  作者: Mango


在本章中,我们将创建一个新的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代码保存到C:\ rspec_tutorial \ spec文件夹中名为string_analyzer.rb的文件中。

这是我们的规范文件来测试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      .
09/13/2015 08:22 AM      ..
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 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 '

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方法测试各种类型的输入字符串将是有意义的。

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

Input string Description Expected result with has_vowels?
‘aaa’, ‘eee’, ‘iii’, ‘o’ Only one vowel and no other letters. true
‘abcefg’ ‘At least one vowel and some consonants’ true
‘mnklp’ Only consonants. false
‘’ Empty string (no letters) false
‘abcde55345&??’ Vowels, consonants, numbers and punctuation characters. true
‘423432%%%^&’ Numbers and punctuation characters only. false
‘AEIOU’ Upper case vowels only. true
‘AeiOuuuA’ Upper case and lower vowels only. true
‘AbCdEfghI’ Upper and lower case vowels and consonants. true
‘BCDFG’ Upper case consonants only. false
‘ ‘ Whitespace characters only. false

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

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

Sr.No. Option/flag & Description
1

-I PATH

Adds PATH to the load (require) path that rspec uses when looking for Ruby source files.

2

-r, –require PATH

Adds a specific source file to be required in your spec. file(s).

3

–fail-fast

With this option, rspec will stop running specs after the first Example fails. By default, rspec runs all specified spec files, no matter how many failures there are.

4

-f, –format FORMATTER

This option allows you to specify different output formats. See the section on Formatters for more details about output formats.

5

-o, –out FILE

This option directs rspec to write the test results to the output file FILE instead of to standard out.

6

-c, –color

Enables color in rspec’s output. Successful Example results will display in green text, failures will print in red text.

7

-b, –backtrace

Displays full error backtraces in rspec’s output.

8

-w, –warnings

Displays Ruby warnings in rspec’s output.

9

-P, –pattern PATTERN

Load and run spec files that match the pattern PATTERN. For example, if you pass -p “*.rb”, rspec will run all Ruby files, not just the ones that end in “_spec.rb”.

10

-e, –example STRING

This option directs rspec to run all Examples that contain the text STRING in their descriptions.

11

-t, –tag TAG

With this option, rspec will only run examples that contain the tag TAG. Note that TAG is specified as a Ruby symbol. See the section on RSpec Tags for more details.