Rspec Tutorial - vigoroom
Ch 00: Introduction
Step 01: mkdir rspec_tutorial
Step 02: cd rspec_tutorial
Step 03: mkdir spec
We are going to store our RSpec files in this folder. RSpec files are known as “specs”. If this seems confusing to you, you can think of a spec file as a test file. RSpec uses the term “spec” which is a short form for “specification”.
Step 04: Let’s return to our Hello World code.
Let’s return to our Hello World code. Open a text editor and add the following code −
class HelloWorld def say_hello "Hello World!" end end describe HelloWorld do context “When testing the HelloWorld class” do it "should say 'Hello World' when we call the say_hello method" do hw = HelloWorld.new message = hw.say_hello expect(message).to eq "Hello World!" end end end
Step 05: save this to a file named hello_world_spec.rb in the spec folder that you created above.
Step 06: Run this command
rspec spec spec\hello_world_spec.rb
Step 07: Output of the Command
Finished in 0.002 seconds (files took 0.11101 seconds to load) 1 example, 0 failures
Step 08: Congratulations, you just created and ran your first RSpec unit test!
Writing Rspec
Step 01: We will create a new Ruby class
Step 02: save it in its own file
Step 03: create a separate spec file to test this class
Ch01: Rspec Basic Syntax
Hello World Example
describe HelloWorld docontext “When testing the HelloWorld class” doit "The say_hello method should return 'Hello World'" dohw = HelloWorld.newmessage = hw.say_helloexpect(message).to eq "Hello World!"endendend
Describe Keyword:
..........................................................................
It is used to define an example group,can think this example group as a collection of tests
The describe keyword can take a class name and/or string argument.
You also need to pass a block argument to describe, this will contain the individual tests, or as they are known in RSpec,
The block is just a Ruby block designated by the Ruby do/end keywords.
..........................................................................
Context Keyword
..........................................................................
The context keyword is similar to describe.
It too can accept a class name and/or string argument.
You should use a block with context as well.
The idea of context is that it encloses tests of a certain type.
The context keyword is not mandatory, but it helps to add more details about the examples that it contains.
For example, you can specify groups of Examples with different contexts like this −
context “When passing bad parameters to the foobar() method” context “When passing valid parameters to the foobar() method” context “When testing corner cases with the foobar() method”
..........................................................................
it Keyword
..........................................................................
The word it is another RSpec keyword which is used to define an “Example”.
An example is basically a test or a test case.
Again, like describe and context, it accepts both class name and string arguments and should be used with a block argument, designated with do/end.
In the case of it, it is customary to only pass a string and block argument.
The string argument often uses the word “should” and is meant to describe what specific behavior should happen inside the it block.
Note the it block from our HelloWorld Example −
it "The say_hello method should return 'Hello World'" do
The string makes it clear what should happen when we call say hello on an instance of the HelloWorld class.
..........................................................................
00 Expect Keyword
The expect keyword is used to define an “Expectation” in RSpec.
This is a verification step where we check, that a specific expected condition has been met.
01 Describe Keyword :
It is used to define an “Example Group”.
You can think of an “Example Group” as a collection of tests.
02 Context Keyword : It encloses tests of a certain type
This is not mandatory but it helps to add more details
about the examples that it contains.
context “When passing bad parameters to the foobar() method”context “When passing valid parameters to the foobar() method”context “When testing corner cases with the foobar() method”
03 It Keyword :This is used for what specific behaviour should used inside the it block
it "The say_hello method should return 'Hello World'" do
04 Expect Keyword :This is a Expectation,this is the verification step where we can check that a specific condition has been met.
expect(message).to eql "Hello World!"
A.
B.
Ch02: Writing Rspec
Step 01: We will create a new Ruby class
Step 02: save it in its own file
Step 03: create a separate spec file to test this class
A. First we will write String Analyzer to test the string
First, in our new class, it is called StringAnalyzer. It’s a simple class that, you guessed it, analyzes strings. Our class has only one method has_vowels? which as its names suggests, returns true if a string contains vowels and false if it doesn’t. Here’s the implementation for StringAnalyzer −
class StringAnalyzer def has_vowels?(str) !!(str =~ /[aeio]+/i) end end
Comments
Post a Comment