RSpec: Defining helper methods for an example group

Updated . Posted . Visible to the public. Repeats.

You can define methods in any example group using Ruby's def keyword or define_method method:

describe "example" do
  def sum(a, b)
    a + b
  end

  it "has access to methods defined in its group" do
    expect(sum(3, 4)).to be(7)
  end
end

The helper method is also available to groups nested within that group. The helper method is not available to parent or sibling groups.

Global helpers

To define helpers for all specs (or all specs of a type), define it in a module Show archive.org snapshot and register it with RSpec.configure.

Henning Koch
Last edit
Henning Koch
Keywords
ruby
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2012-12-13 09:56)