woensdag 27 mei 2015

TDD RSpec Shared Examples

When writing shared examples, I often run into situations that the shared examples don't work as expected, since they mostly go untested. As I found no real examples of how to achieve this, I dived into the Rspec code and started my own experiments. So here's whatI came up with: (most of the times you're interested that the tests final when they are supposed to fail, so that's this idea about this test)

RSpec.shared_examples 'an errored resource' do |attributes|
  it 'always fails' do
    fail 'what did you expect?'
  end
end

To test this nice block of code, this is my test:


describe 'an errored resource' do
  let(:example_group) do
    RSpec.describe('group description')
  end

  it "fails the shared example" do
    example_group.include_examples('an errored resource')
    example_group.run
    example = example_group.examples.find { |x| x.description == 'always fails' }
    expect(example.exception.message).to eq('what did you expect?')
  end
end

So the test passes nicely...

Geen opmerkingen:

Een reactie posten