Module: Spec::DSL::BehaviourEval::ModuleMethods

Includes:
Spec::DSL::BehaviourCallbacks
Defined in:
lib/spec/dsl/behaviour_eval.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Spec::DSL::BehaviourCallbacks

#add, #after_all_parts, #after_each_parts, #append_after, #append_before, #before_all_parts, #before_each_parts, #clear_before_and_after!, #prepend_after, #prepend_before, #remove_after, #scope_and_options, #setup, #teardown

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (protected)



133
134
135
136
137
138
# File 'lib/spec/dsl/behaviour_eval.rb', line 133

def method_missing(method_name, *args)
  if behaviour_superclass.respond_to?(method_name)
    return execution_context_class.send(method_name, *args)
  end
  super
end

Instance Attribute Details

#behaviour=(value) ⇒ Object (writeonly)

Sets the attribute behaviour

Parameters:

  • value

    the value to set the attribute behaviour to.



7
8
9
# File 'lib/spec/dsl/behaviour_eval.rb', line 7

def behaviour=(value)
  @behaviour = value
end

#descriptionObject

Returns the value of attribute description.



8
9
10
# File 'lib/spec/dsl/behaviour_eval.rb', line 8

def description
  @description
end

Instance Method Details

#copy_to(eval_module) ⇒ Object

:nodoc:



59
60
61
62
63
64
65
66
67
# File 'lib/spec/dsl/behaviour_eval.rb', line 59

def copy_to(eval_module) # :nodoc:
  examples.each          { |e| eval_module.examples << e; }
  before_each_parts.each { |p| eval_module.before_each_parts << p }
  after_each_parts.each  { |p| eval_module.after_each_parts << p }
  before_all_parts.each  { |p| eval_module.before_all_parts << p }
  after_all_parts.each   { |p| eval_module.after_all_parts << p }
  included_modules.each  { |m| eval_module.included_modules << m }
  eval_module.included_modules << self
end

#define_predicate_matchers(hash = nil) ⇒ Object

:nodoc:



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/spec/dsl/behaviour_eval.rb', line 101

def define_predicate_matchers(hash=nil) # :nodoc:
  if hash.nil?
    define_predicate_matchers(predicate_matchers)
    define_predicate_matchers(Spec::Runner.configuration.predicate_matchers)
  else
    hash.each_pair do |matcher_method, method_on_object|
      define_method matcher_method do |*args|
        eval("be_#{method_on_object.to_s.gsub('?','')}(*args)")
      end
    end
  end
end

#include(*mods) ⇒ Object

You can pass this one or many modules. Each module will subsequently be included in the each object in which an example is run. Use this to provide global helper methods to your examples.

Example

module HelperMethods
  def helper_method
    ...
  end
end

describe Thing do
  include HelperMethods
  it "should do stuff" do
    helper_method
  end
end


42
43
44
45
46
47
# File 'lib/spec/dsl/behaviour_eval.rb', line 42

def include(*mods)
  mods.each do |mod|
    included_modules << mod
    mod.send :included, self
  end
end

#inherit(klass) ⇒ Object

RSpec runs every example in a new instance of Object, mixing in the behaviour necessary to run examples. Because this behaviour gets mixed in, it can get mixed in to an instance of any class at all.

This is something that you would hardly ever use, but there is one common use case for it - inheriting from Test::Unit::TestCase. RSpec’s Rails plugin uses this feature to provide access to all of the features that are available for Test::Unit within RSpec examples.

Raises:

  • (ArgumentError)


18
19
20
21
22
# File 'lib/spec/dsl/behaviour_eval.rb', line 18

def inherit(klass)
  raise ArgumentError.new("Shared behaviours cannot inherit from classes") if @behaviour.shared?
  @behaviour_superclass = klass
  derive_execution_context_class_from_behaviour_superclass
end

#it(description = :__generate_description, opts = {}, &block) ⇒ Object

Creates an instance of Spec::DSL::Example and adds it to a collection of examples of the current behaviour.



116
117
118
# File 'lib/spec/dsl/behaviour_eval.rb', line 116

def it(description=:__generate_description, opts={}, &block)
  examples << Example.new(description, opts, &block)
end

#it_should_behave_like(behaviour_description) ⇒ Object

Use this to pull in examples from shared behaviours. See Spec::Runner for information about shared behaviours.



51
52
53
54
55
56
57
# File 'lib/spec/dsl/behaviour_eval.rb', line 51

def it_should_behave_like(behaviour_description)
  behaviour = @behaviour.class.find_shared_behaviour(behaviour_description)
  if behaviour.nil?
    raise RuntimeError.new("Shared Behaviour '#{behaviour_description}' can not be found")
  end
  behaviour.copy_to(self)
end

#methodsObject

:nodoc:



125
126
127
128
129
# File 'lib/spec/dsl/behaviour_eval.rb', line 125

def methods # :nodoc:
  my_methods = super
  my_methods |= behaviour_superclass.methods
  my_methods
end

#predicate_matchersObject

:call-seq:

predicate_matchers[matcher_name] = method_on_object
predicate_matchers[matcher_name] = [method1_on_object, method2_on_object]

Dynamically generates a custom matcher that will match a predicate on your class. RSpec provides a couple of these out of the box:

exist (or state expectations)
  File.should exist("path/to/file")

an_instance_of (for mock argument constraints)
  mock.should_receive(:message).with(an_instance_of(String))

Examples

class Fish
  def can_swim?
    true
  end
end

describe Fish do
  predicate_matchers[:swim] = :can_swim?
  it "should swim" do
    Fish.new.should swim
  end
end


97
98
99
# File 'lib/spec/dsl/behaviour_eval.rb', line 97

def predicate_matchers
  @predicate_matchers ||= {:exist => :exist?, :an_instance_of => :is_a?}
end

#specify(description = :__generate_description, opts = {}, &block) ⇒ Object

Alias for it.



121
122
123
# File 'lib/spec/dsl/behaviour_eval.rb', line 121

def specify(description=:__generate_description, opts={}, &block)
  it(description, opts, &block)
end