Module: ActiveSpec::Context

Defined in:
lib/active_spec/context.rb

Overview

Provides a high-level DSL-like interface to the ActiveSpec library.

Instance Method Summary collapse

Instance Method Details

#specification(name, &block) ⇒ Object

This method allows you to build ActiveSpec specifications using a DSL-like syntax. Pass in a name for your specification (without the _specification suffix which will be added automatically) and a block which contains your individual specifications.

The ActiveSpec::Context module gets included automatically when the ActiveSpec library is required, so the specification method is available in the global namespace.

Example usage:

specification :user do
  should_require_presence_of     :username, :password
  should_require_confirmation_of :password
end

The above code will create a UserSpecification class (a sub-class of ActiveSpec::Base) with the given specifications.



24
25
26
27
28
# File 'lib/active_spec/context.rb', line 24

def specification(name, &block)
  klass = Inflector::classify("#{name.to_s}_specification")
  Object.const_set(klass, Class.new(ActiveSpec::Base))
  Object.const_get(klass).class_eval(&block)
end