Module: Setsuzoku::DynamicSpecHelper

Defined in:
lib/setsuzoku/rspec/dynamic_spec_helper.rb

Instance Method Summary collapse

Instance Method Details

#register_all_plugin_tests_and_stubs(plugin_class: self.described_class, subclasses_to_register: plugin_class.subclasses, registering_instance: nil, stub_directory: nil) ⇒ Object

EXTERNAL PLUGIN SPEC HELPERS ###

These specs are designed to test and ensure that a plugin’s implementations uphold their contract. Additionally they ensure that a plugin’s blackbox stubbing also upholds their contract.

This will register plugin specs for a base plugin class that defines its interfaces.

It will register specs to ensure the base plugin’s black box stubs are correct.

It will also register specs for all implementations of the plugin to ensure the implementations are valid. (This is similar to register_plugin_tests_and_stubs)



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/setsuzoku/rspec/dynamic_spec_helper.rb', line 22

def register_all_plugin_tests_and_stubs(plugin_class: self.described_class, subclasses_to_register: plugin_class.subclasses, registering_instance: nil, stub_directory: nil)
  context "*Dynamic Specs* for #{plugin_class.name}: Stub Definitions" do
    let(:plugin){ plugin_class.new }
    plugin_class.spec_definitions.each do |action_name, spec|
      define_stub_plugin_method(plugin_class, action_name, spec)
      it(spec[:name], action_name: action_name, &spec[:spec])
    end
  end

  perform_register(plugin_class, registering_instance: registering_instance, subclasses_to_register: subclasses_to_register, stub_directory: stub_directory)
end

#register_plugin_tests_and_stubs(plugin_class, registering_instance: nil, stub_directory: nil) ⇒ Object

This will register plugin specs for a single plugin class. The specs will test the plugin to ensure its implementations are valid.



36
37
38
# File 'lib/setsuzoku/rspec/dynamic_spec_helper.rb', line 36

def register_plugin_tests_and_stubs(plugin_class, registering_instance: nil, stub_directory: nil)
  perform_register(plugin_class, registering_instance: registering_instance, stub_directory: stub_directory)
end

#stub_all_plugin_methods(registering_class = self.described_class) ⇒ Object

APPLICATION SPEC HELPERS ###

These stubs completely black box the plugin’s functionality and returns a stubbed response without invoking the plugin’s method.

Stub all plugin interface methods at a context level for an application class that registers a plugin. This also defines a before each to execute the stubs.

Parameters:

  • registering_class (Array/Class) (defaults to: self.described_class)

    the application’s class/es that register a plugin and need methods stubbed.

Returns:

  • void



54
55
56
57
58
59
60
61
62
# File 'lib/setsuzoku/rspec/dynamic_spec_helper.rb', line 54

def stub_all_plugin_methods(registering_class = self.described_class)
  registering_class = [registering_class] unless registering_class.is_a?(Array)
  registering_class.each do |klass|
    plugin_class = klass.plugin_context[:plugin_class]
    plugin_class.spec_definitions.each do |action_name, spec|
      define_stub_plugin_method(plugin_class, action_name, spec, true)
    end
  end
end

#stub_plugin_methods_for_spec(registering_class = self.described_class) ⇒ Object

Stub all plugin interface methods for an application class that registers a plugin for a single spec.

Parameters:

  • registering_class (Array/Class) (defaults to: self.described_class)

    the application’s class/es that register a plugin and need methods stubbed.

Returns:

  • void



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/setsuzoku/rspec/dynamic_spec_helper.rb', line 69

def stub_plugin_methods_for_spec(registering_class = self.described_class)
  plugin_class = registering_class.plugin_context[:plugin_class]
  plugin_class.spec_definitions.each do |action_name, spec|
    plugin_class.any_instance.stub(action_name).and_return(spec[:stub][:response])
    # a plugin spec might have some dynamic data, e.g. certain methods are called with plugin specific args.
    if spec[:stub][:dynamic_methods]
      spec[:stub][:dynamic_methods].each do |meth, resp|
        #how can we ignore the call count instead of just putting a high limit?
        plugin_class.any_instance.stub(meth).and_return(resp)
      end
    end
  end
end