GitHub

title RSpec
category Ruby

About

{: .-intro}

RSpec is a Ruby library for testing.

Invoking tests

rake -T spec      # List spec tasks
rake spec         # Run all
rake spec/models/mymodel_spec.rb
rake spec/models/mymodel_spec.rb:27

Writing tests

describe "A User (in general)" do
  include UserSpecHelper
  subject { Person.new }
  let(:admin) { Person.new(role: :admin) }
  context "setter methods" do
    it "should do this" do
      pending "some other thing"
      expect(subject.name).to eq 'x'
    end
  end
end

Before/after

before :each do
  # before all tests
end
before :all do
  # before this suite
end
after : all do
  # after this suite
end

Subjects

subject { CheckingAccount.new }
it { is_expected.to be_empty }
# also names: subject(:account) { ... }

Expectations

target.should eq 1
target.should_not eq 1
expect(target).to eq 1
expect(target).not_to eq 1

Numeric

expect(5).to be < 6
expect(5).to == 5
expect(5).to equal value
expect(5).to be_between(1, 10)
expect(5).to be_within(0.05).of value

Compound expectations

expect(1).to (be < 2).or be > 5

Use or/and to string multiple matchers together. See: Compound expectations

Comparison

expect(x).to be value
expect(x).to satisfy { |arg| ... }
expect(x).to match /regexp/

Predicate

expect(x).to be_zero    # FixNum#zero?
expect(x).to be_empty   # Array#empty?
expect(x).to have_key   # Hash#has_key?

Objects

expect(obj).to be_an_instance_of MyClass
expect(obj).to be_a_kind_of MyClass
expect(obj).to respond_to :save!

Control flow

expect { user.save! }.to raise_error
expect { user.save! }.to raise_error(ExceptionName, /msg/)
expect { user.save! }.to throw :symbol

Enumerables/arrays

"expect(list).to include(

Read the original on github.com ↗