RSpec Matchers

by woody2shoes on June 3, 2010


RSpec gives us many powerful tools to make our tests readable. Matchers allow us to provide custom predicates to our should statements that succinctly define the behavior of our code.
Download 27 MB
Download (iphone & ipod) 14 MB

  • http://hosheng.blogspot.com/ Ho-Sheng Hsiao

    I like this video. I do want to point out, you *can* actually use include as a matcher in rspec:

    array.should include(element) has worked for me since I started using rspec about two years ago.

  • http://hosheng.blogspot.com/ Ho-Sheng Hsiao

    I like this video. I do want to point out, you *can* actually use include as a matcher in rspec:

    array.should include(element) has worked for me since I started using rspec about two years ago.

  • http://teachmetocode.com Charles Max Wood

    Aha! Good to know.

  • woody2shoes

    Aha! Good to know.

  • http://www.arctickiwi.com Jonathon

    Cool vid, thanks very much. I just wrote the “include_text” matcher:

    module RSpec::Rails
    module Matchers
    RSpec::Matchers.define :include_text do |text|
    match do |response_or_text|
    @content = response_or_text.respond_to?(:body) ? response_or_text.body : response_or_text
    @content.include?(text)
    end

    failure_message_for_should do |text|
    “expected ‘#{@content}’ to contain ‘#{text}’”
    end

    failure_message_for_should_not do |text|
    “expected #{@content} to not contain ‘#{text}’”
    end
    end
    end
    end

  • http://www.arctickiwi.com Jonathon Horsman

    Cool vid, thanks very much. I just wrote the “include_text” matcher:

    module RSpec::Rails
    module Matchers
    RSpec::Matchers.define :include_text do |text|
    match do |response_or_text|
    @content = response_or_text.respond_to?(:body) ? response_or_text.body : response_or_text
    @content.include?(text)
    end

    failure_message_for_should do |text|
    “expected ‘#{@content}’ to contain ‘#{text}’”
    end

    failure_message_for_should_not do |text|
    “expected #{@content} to not contain ‘#{text}’”
    end
    end
    end
    end

Previous post:

Next post: