Skip to content

Instantly share code, notes, and snippets.

@jayzes
Created March 7, 2014 00:01

Revisions

  1. Jay Zeschin created this gist Mar 7, 2014.
    34 changes: 34 additions & 0 deletions given.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    module GivenWhenThen

    def Given(*args, &block)
    if args.first.is_a?(Symbol)
    let(*args, &block)
    else
    before(&block)
    end
    end

    def Given!(*args, &block)
    let!(*args, &block)
    end

    def When(*args, &block)
    before(&block)
    end

    def Then(&block)
    unless @queued_examples
    @queued_examples = []
    __examples = @queued_examples
    it '' do
    __examples.map { |e| self.instance_exec(&e) }
    end
    end
    @queued_examples << block
    end

    alias_method :And, :Then

    end

    RSpec::Core::ExampleGroup.send :extend, GivenWhenThen