Home

Intro to RSpec

tl;dr

Helpful link for rails testing here.

Ensure rspec gem installed.

# calculator.rb class Calculator def add(x, y) x + y end end # calculator_spec.rb require './calculator.rb' describe "calculator" do it "adds numbers" do calc = Calculator.new expect(calc.add(2,3)).to eql(5) end end

Run rspec calculator_spec.rb.

Setting up a calculator file

# calculator.rb class Calculator def add(x, y) x + y end end

Pending tests

require './calculator.rb' describe "calculator" do it "adds numbers" end

Running rspec calculator_spec.rb should come back with 1 pending test.

Adding your first test

require './calculator.rb' describe "calculator" do it "adds numbers" do calc = Calculator.new expect(calc.add(2,3)).to eql(5) end end

Running rspec calculator_spec.rb should come back with 1 successful test.

Module testing a Ruby Symbol

# flags.rb module Flags Truthy = :truthy end # flags_spec.rb require './flags.rb' describe 'for flag module' do context 'when ruby symbol is involved' do subject { Flags::Truthy } it { expect(subject).to eq(:truthy)} end end

rspec flags_spec.rb

Repository

https://github.com/okeeffed/developer-notes-nextjs/content/rspec/rspec-intro

Sections


Related