RSpec - Composing Matchers

03/05/15  Public, Testing / TDD

describe 'composing matchers' do
  # some matchers accept matchers as arguments. (new in rspec3)

  it 'will match all collection elements using a matcher' do
    array = [1,2,3]
    expect(array).to all( be < 5 )
  end

  it ...

1 vote - harrylevine

RSpec - Compound Matchers

03/05/15  Public, Testing / TDD

describe 'compound expectations' do

  it 'will match using: and, or, &, |' do
    expect([1,2,3,4]).to start_with(1).and end_with(4)

    expect([1,2,3,4]).to start_with(1) & include(2)

    expect(10 * 10).to be_odd.or be >...

1 vote - harrylevine

RSpec - Observation Matchers

03/05/15  Public, Testing / TDD

describe 'observation matchers' do
  # Note that all of these use "expect {}", not "expect()".
  # It is a special block format that allows a 
  # process to take place inside of the expectation.

  it 'will match when events chang...

1 vote - harrylevine

RSpec - Predicate Matchers

03/05/15  Public, Testing / TDD

describe 'predicate matchers' do

    it 'will match be_* to custom methods ending in ?' do
      # drops "be_", adds "?" to end, calls method on object
      # Can use these when methods end in "?", require no arguments, 
      ...

1 vote - harrylevine

RSpec - Other Useful Matchers

03/05/15  Public, Testing / TDD

describe 'other useful matchers' do

  it 'will match strings with a regex' do
    # This matcher is a good way to "spot check" strings
    string = 'The order has been received.'
    expect(string).to match(/order(.+)received/)

 ...

1 vote - harrylevine

RSpec - Collection Matchers

03/05/15  Public, Testing / TDD

describe 'collection matchers' do

  it 'will match arrays' do
    array = [1,2,3]

    expect(array).to include(3)
    expect(array).to include(1,3)

    expect(array).to start_with(1)
    expect(array).to end_with(3)
   ...

1 vote - harrylevine

RSpec - Numeric Comparison Matchers

03/05/15  Public, Testing / TDD

describe 'numeric comparison matchers' do

  it 'will match less than/greater than' do
    expect(10).to be > 9
    expect(10).to be >= 10
    expect(10).to be <= 10
    expect(9).to be < 10
  end

  it 'will match numeric ranges...

1 vote - harrylevine

RSpec - Truthiness Matchers

03/05/15  Public, Testing / TDD

describe 'truthiness matchers' do

  it 'will match true/false' do
    expect(1 < 2).to be(true)       # do not use 'be_true'
    expect(1 > 2).to be(false)      # do not use 'be_false'

    expect('foo').not_to be(true)   # the str...

1 vote - harrylevine

RSpec - Equivalence Matchers

03/05/15  Public, Testing / TDD

describe 'equivalence matchers' do

  it 'will match loose equality with #eq' do
    a = "2 cats"
    b = "2 cats"
    expect(a).to eq(b)
    expect(a).to be == b      # synonym for #eq

    c = 17
    d = 17.0
    expect(c).to ...

1 vote - harrylevine

Using the .class method to determine if an object is what you are looking for

05/28/15  Public, Tricks, Ruby General

Let's say that you want to use a given object if the object is an array; however, if the object is not an array (meaning it is a string, or a fixnum, etc.), then you do not want to use it.

You can use the .class method with conditional l...

1 vote - harrylevine