Module | Spec::Example::ExampleGroupMethods |
In: |
lib/spec/example/example_group_methods.rb
|
description_args | [R] | |
description_options | [R] | |
description_text | [R] | |
spec_path | [R] |
# File lib/spec/example/example_group_methods.rb, line 6 6: def description_text(*args) 7: args.inject("") do |result, arg| 8: result << " " unless (result == "" || arg.to_s =~ /^(\s|\.|#)/) 9: result << arg.to_s 10: end 11: end
Registers a block to be executed after each example. This method appends block to existing after blocks.
# File lib/spec/example/example_group_methods.rb, line 195 195: def append_after(*args, &block) 196: scope, options = scope_and_options(*args) 197: parts = after_parts_from_scope(scope) 198: parts << block 199: end
Registers a block to be executed before each example. This method appends block to existing before blocks.
# File lib/spec/example/example_group_methods.rb, line 177 177: def append_before(*args, &block) 178: scope, options = scope_and_options(*args) 179: parts = before_parts_from_scope(scope) 180: parts << block 181: end
Makes the describe/it syntax available from a class. For example:
class StackSpec < Spec::ExampleGroup describe Stack, "with no elements" before @stack = Stack.new end it "should raise on pop" do lambda{ @stack.pop }.should raise_error end end
# File lib/spec/example/example_group_methods.rb, line 35 35: def describe(*args, &example_group_block) 36: if example_group_block 37: self.subclass("Subclass") do 38: describe(*args) 39: module_eval(&example_group_block) 40: end 41: else 42: set_description(*args) 43: before_eval 44: self 45: end 46: end
# File lib/spec/example/example_group_methods.rb, line 133 133: def described_type 134: description_parts.find {|part| part.is_a?(Module)} 135: end
# File lib/spec/example/example_group_methods.rb, line 124 124: def description 125: result = ExampleGroupMethods.description_text(*description_parts) 126: if result.nil? || result == "" 127: return to_s 128: else 129: result 130: end 131: end
# File lib/spec/example/example_group_methods.rb, line 16 16: def inherited(klass) 17: super 18: klass.register 19: end
Creates an instance of Spec::Example::Example and adds it to a collection of examples of the current example group.
# File lib/spec/example/example_group_methods.rb, line 97 97: def it(description=nil, &implementation) 98: e = new(description, &implementation) 99: example_objects << e 100: e 101: end
Use this to pull in examples from shared example groups. See Spec::Runner for information about shared example groups.
# File lib/spec/example/example_group_methods.rb, line 50 50: def it_should_behave_like(shared_example_group) 51: case shared_example_group 52: when SharedExampleGroup 53: include shared_example_group 54: else 55: example_group = SharedExampleGroup.find_shared_example_group(shared_example_group) 56: unless example_group 57: raise RuntimeError.new("Shared Example Group '#{shared_example_group}' can not be found") 58: end 59: include(example_group) 60: end 61: 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/example/example_group_methods.rb, line 91 91: def predicate_matchers 92: @predicate_matchers ||= {:an_instance_of => :is_a?} 93: end
Registers a block to be executed after each example. This method prepends block to existing after blocks.
# File lib/spec/example/example_group_methods.rb, line 186 186: def prepend_after(*args, &block) 187: scope, options = scope_and_options(*args) 188: parts = after_parts_from_scope(scope) 189: parts.unshift(block) 190: end
Registers a block to be executed before each example. This method prepends block to existing before blocks.
# File lib/spec/example/example_group_methods.rb, line 169 169: def prepend_before(*args, &block) 170: scope, options = scope_and_options(*args) 171: parts = before_parts_from_scope(scope) 172: parts.unshift(block) 173: end
# File lib/spec/example/example_group_methods.rb, line 239 239: def register 240: rspec_options.add_example_group self 241: end
# File lib/spec/example/example_group_methods.rb, line 201 201: def remove_after(scope, &block) 202: after_each_parts.delete(block) 203: end
# File lib/spec/example/example_group_methods.rb, line 110 110: def run 111: examples = examples_to_run 112: return true if examples.empty? 113: reporter.add_example_group(self) 114: return dry_run(examples) if dry_run? 115: 116: plugin_mock_framework 117: define_methods_from_predicate_matchers 118: 119: success, before_all_instance_variables = run_before_all 120: success, after_all_instance_variables = execute_examples(success, before_all_instance_variables, examples) 121: success = run_after_all(success, after_all_instance_variables) 122: end
# File lib/spec/example/example_group_methods.rb, line 253 253: def run_after_each(example) 254: execute_in_class_hierarchy(:superclass_first) do |example_group| 255: example.eval_each_fail_slow(example_group.after_each_parts) 256: end 257: end
# File lib/spec/example/example_group_methods.rb, line 247 247: def run_before_each(example) 248: execute_in_class_hierarchy do |example_group| 249: example.eval_each_fail_fast(example_group.before_each_parts) 250: end 251: end
# File lib/spec/example/example_group_methods.rb, line 145 145: def set_description(*args) 146: args, options = args_and_options(*args) 147: @description_args = args 148: @description_options = options 149: @description_text = ExampleGroupMethods.description_text(*args) 150: @spec_path = File.expand_path(options[:spec_path]) if options[:spec_path] 151: if described_type.class == Module 152: include described_type 153: end 154: self 155: end