| Module | Spec::DSL::BehaviourEval::ModuleMethods |
| In: |
lib/spec/dsl/behaviour_eval.rb
|
| behaviour | [W] | |
| description | [RW] |
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.
module HelperMethods
def helper_method
...
end
end
describe Thing do
include HelperMethods
it "should do stuff" do
helper_method
end
end
# File lib/spec/dsl/behaviour_eval.rb, line 42
42: def include(*mods)
43: mods.each do |mod|
44: included_modules << mod
45: mod.send :included, self
46: end
47: end
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.
# File lib/spec/dsl/behaviour_eval.rb, line 18
18: def inherit(klass)
19: raise ArgumentError.new("Shared behaviours cannot inherit from classes") if @behaviour.shared?
20: @behaviour_superclass = klass
21: derive_execution_context_class_from_behaviour_superclass
22: end
Creates an instance of Spec::DSL::Example and adds it to a collection of examples of the current behaviour.
# File lib/spec/dsl/behaviour_eval.rb, line 116
116: def it(description=:__generate_description, opts={}, &block)
117: examples << Example.new(description, opts, &block)
118: end
Use this to pull in examples from shared behaviours. See Spec::Runner for information about shared behaviours.
# File lib/spec/dsl/behaviour_eval.rb, line 51
51: def it_should_behave_like(behaviour_description)
52: behaviour = @behaviour.class.find_shared_behaviour(behaviour_description)
53: if behaviour.nil?
54: raise RuntimeError.new("Shared Behaviour '#{behaviour_description}' can not be found")
55: end
56: behaviour.copy_to(self)
57: end
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))
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
# File lib/spec/dsl/behaviour_eval.rb, line 97
97: def predicate_matchers
98: @predicate_matchers ||= {:exist => :exist?, :an_instance_of => :is_a?}
99: end
# File lib/spec/dsl/behaviour_eval.rb, line 158
158: def after_all_proc(behaviour_type)
159: parts = []
160: parts.push(*after_all_parts(behaviour_type)) unless behaviour_type.nil?
161: parts.push(*after_all_parts(nil))
162: parts.push(*Behaviour.after_all_parts(behaviour_type)) unless behaviour_type.nil?
163: parts.push(*Behaviour.after_all_parts(nil))
164: CompositeProcBuilder.new(parts).proc
165: end
# File lib/spec/dsl/behaviour_eval.rb, line 167
167: def after_each_proc(behaviour_type)
168: parts = []
169: parts.push(*after_each_parts(behaviour_type)) unless behaviour_type.nil?
170: parts.push(*after_each_parts(nil))
171: parts.push(*Behaviour.after_each_parts(behaviour_type)) unless behaviour_type.nil?
172: parts.push(*Behaviour.after_each_parts(nil))
173: CompositeProcBuilder.new(parts).proc
174: end
# File lib/spec/dsl/behaviour_eval.rb, line 149
149: def before_all_proc(behaviour_type, &error_handler)
150: parts = []
151: parts.push(*Behaviour.before_all_parts(nil))
152: parts.push(*Behaviour.before_all_parts(behaviour_type)) unless behaviour_type.nil?
153: parts.push(*before_all_parts(nil))
154: parts.push(*before_all_parts(behaviour_type)) unless behaviour_type.nil?
155: CompositeProcBuilder.new(parts).proc(&error_handler)
156: end
# File lib/spec/dsl/behaviour_eval.rb, line 140
140: def before_each_proc(behaviour_type, &error_handler)
141: parts = []
142: parts.push(*Behaviour.before_each_parts(nil))
143: parts.push(*Behaviour.before_each_parts(behaviour_type)) unless behaviour_type.nil?
144: parts.push(*before_each_parts(nil))
145: parts.push(*before_each_parts(behaviour_type)) unless behaviour_type.nil?
146: CompositeProcBuilder.new(parts).proc(&error_handler)
147: end
# File lib/spec/dsl/behaviour_eval.rb, line 193
193: def included_modules
194: @included_modules ||= [::Spec::Matchers]
195: end