RSpec is a testing tool for Ruby to create behavior-driven development (BDD). RSpec is made up of multiple libraries that are designed to work together or can be used independently with other testing tools like Cucumber or Minitest. The parts are:
RSpec is documented through executable examples on Relish: http:relishapp.com/rspec
The examples are written in an “end-to-end” style demonstrating the use of various RSpec features in the context of executable spec files. If you want detailed documentation about a particular API or feature, use the API docs instead.
Cucumber provides executable documentation and provides reports indicating whether the software behaves according to the specification or not.
BDD’s parent is test-driven development (TDD), which means we work in a red-green loop for writing small tests so that the test initially fails, then passes after writing our code.
BDD is a concept build on top of TDD, where we write tests as specifications of system behavior. With RSpec, we are describing the behavior of classes, modules, and their methods.
specs
(specifications)Run specs with bundle exec rspec
https://github.com/colszowka/simplecov
Install the simplecov
gem, then add SimpleCov to the top of your spec_helper.rb
file.
require 'simplecov'
SimpleCov.start
You should then get a directory called coverage
with your results
One thing to note is that you want to put SimpleCov above everything else. For example, if you have some code
require 'mycode'
that you want covered, make sure that require 'simplecov'
and SimpleCov.start
is above your
other require.
So RSpec is a behavior-driven development process of writing human readable specifications.
The basic structure is that Rspec uses the words describe
and it
so we can express concepts like a conversation:
describe
and it
describe
and context
describe
and context
group related tests together
They are the same method.
So when do you use describe
and when do you use context
?
describe
for thingscontext
for statesBad: context "#matriculate"
Better:
describe “#matriculate”
Bad: describe "when the student is sick"
Better: context "when the student is sick"
context
context
since tests usually deal with permutations of statecontext
names should not match your method namesExample:
Bad: describe "#assign"
Better: context "assigning homework to a student"
describe
and/or context
RSpec has two scopes:
describe
or context
blockit
block, which are evaluated in the context of an instance of the
example group class to which the example belongs.Example of a nested group:
RSpec.describe Order do
context "with no items" do
it "behaves one way" do
# ...
end
end
context "with one item" do
it "behaves another way" do
# ...
end
end
end
before
vs let
before
eagerly runs code before each test
let
lazily runs code when it is first used
Use before
for actions
Use let
for dependencies (real or test double)
Examples:
Bad: let(:dummy) do @classroom.initialize_roster end Better: before do @classroom.initialize_roster end
Bad: before { @grade_levels = [1, 2, 3] } Better: let(:grade_levels) { [1, 2, 3] }
before
with context
context
is used for statebefore
lists the actions to get to that stateBad: it “should add the student to the class” Better: it “adds the student to the class”
http://www.betterspecs.org/ https://github.com/rubocop-hq/rspec-style-guide http://jakegoulding.com/presentations/rspec-structure/#slide-1