William Liu

Ruby Rspec Tests

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:

Relish

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

Cucumber provides executable documentation and provides reports indicating whether the software behaves according to the specification or not.

Test Driven Development (TDD)

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.

Behavior Driven Development (BDD)

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.

Run specs with bundle exec rspec

Code coverage with Simplecov

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.

Describing Methods

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?

Bad: context "#matriculate" Better: describe “#matriculate”

Bad: describe "when the student is sick" Better: context "when the student is sick"

context

Example:

Bad: describe "#assign" Better: context "assigning homework to a student"

Nested Groups with describe and/or context

RSpec has two scopes:

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

Remove weak words

Bad: it “should add the student to the class” Better: it “adds the student to the class”

Sources

http://www.betterspecs.org/ https://github.com/rubocop-hq/rspec-style-guide http://jakegoulding.com/presentations/rspec-structure/#slide-1