RSpec Writing Specs - RSpec

How RSpec specs are written?

A new Ruby class is created and saved in its own file. To test this class a separate spec file is created.

The new class StringAnalyzer which analyzes the strings has only one method has_vowels?. The result turns to be true if string contains vowels and fasle if does not. The implementation for StringAnalyzer is −

In the previous chapter, HelloWorld section, a folder C: spec_tutorialspec is created.

Delete the hello_world.rb file if you have it and save the StringAnalyzer code above to a file called string_analyzer.rb in the C: spec_tutorialspec folder.

The source for our spec file to test StringAnalyzer is −

Save this in the same spec directory, by the name string_analyzer_test.rb.

In cmd.exe window, cd to the C: spec_tutorial folder and run this command: dir spec

The output should appear as -

Directory of C: spec_tutorialspec

The tests are run, by running the command : rspec spec

When the name of a folder is passed to to rspec, it runs all of the spec files inside of the folder. The result should appear as −

rspec only runs files whose names end in “_spec.rb”. Rename string_analyzer_test.rb to string_analyzer_spec.rb.This can be done by running the command -

Now, run rspec spec again, the output should appears like -

Now, the changes made in string_analyizer.rb are saved run the rspec spec command again, the output should appear as −

The examples (tests) in spec file are now passing. A bug is fixed in regular expression having vowels method but the tests are far from complete.

More examples can be added to test the types of input strings with vowels method.

Many permutations can be added in new Examples (it blocks). Some of them are summarized in a tabular form as follows:

Input string
Expected result with has_vowels?
‘aaa’, ‘eee’, ‘iii’, ‘o’
Only one vowel and no other letters.
‘At least one vowel and some consonants’
Only consonants.
Empty string (no letters)
Vowels, consonants, numbers and punctuation characters.
Numbers and punctuation characters only.
Upper case vowels only.
Upper case and lower vowels only.
Upper and lower case vowels and consonants.
Upper case consonants only.
‘ ‘
Whitespace characters only.

There are many conditions to test, choose as desired which subset of condition is important and best test the code.

Different options are offered by the rspec command.The following table lists the most popular options and describes what they do.

Adds PATH to the load (require) path thatrspecuses when looking for Ruby source files.
-r, --require PATH
Adds a specific source file to be required in your spec. file(s).
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.
-f, --format FORMATTER
This option allows you to specify different output formats. See the section on Formatters for more details about output formats.
-o, --out FILE
This option directs rspec to write the test results to the output file FILE instead of to standard out.
-c, --color
Enables color in rspec’s output. Successful Example results will display in green text, failures will print in red text.
-b, --backtrace
Displays full error backtraces in rspec’s output.
-w, --warnings
Displays Ruby warnings in rspec’s output.
-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”
-e, --example STRING
This option directs rspec to run all Examples that contain the text STRING in their descriptions.
-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.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

RSpec Topics