class Object
Public Instance Methods
_find_child_with_description(group, description)
click to toggle source
# File lib/rester/rspec.rb, line 197 def _find_child_with_description(group, description) group.children.find { |child_group| child_group.description == description } end
_find_or_create_child(group, description)
click to toggle source
# File lib/rester/rspec.rb, line 203 def _find_or_create_child(group, description) child = _find_child_with_description(group, description) child || group.describe(description) end
_missing_stub_tests(tests)
click to toggle source
Takes a hash produced by _rester_service_tests. Returns a hash of only the missing stub tests:
{
"/v1/tests/abc123/mounted_objects" => { "POST" => { "With some context" => false } }, "/v1/stuff" => { "GET" => { "Doing that" => false } }
}
# File lib/rester/rspec.rb, line 176 def _missing_stub_tests(tests) Hash[@rester_stub.reject { |k, _| ['version', 'consumer', 'producer'].include?(k) }.map { |path, verbs| [ path, Hash[verbs.map { |verb, contexts| [ verb, Hash[contexts.map { |context, _| [ context, !!(tests[path] && tests[path][verb] && tests[path][verb][context]) ] }].reject { |_, v| v } ] }].reject { |_, v| v.empty? } ] }].reject { |_, v| v.empty? } end
_rester_service_tests(parent_example_group)
click to toggle source
# File lib/rester/rspec.rb, line 140 def _rester_service_tests(parent_example_group) service_tests = {} parent_example_group.class.children.each { |path_group| path = path_group.description service_tests[path] ||= {} path_group.children.each { |verb_group| verb = verb_group.description service_tests[path][verb] ||= {} verb_group.children.each { |context_group| context = context_group.description service_tests[path][verb][context] = context_group.examples.count > 0 } } } service_tests end
_setup_example(ex)
click to toggle source
# File lib/rester/rspec.rb, line 54 def _setup_example(ex) # Gather the request args from the spec descriptions # # For example: # # describe '/v1/tests' do # context 'GET' do # context 'With some context' do # end # end # end # # would produce: # # context, verb, path = ['With some context', 'GET', '/v1/tests'] context, verb, path = ex.example_group.parent_groups.map { |a| a.description unless a.metadata[:description] == a.described_class.to_s }.compact begin spec = @rester_stub[path][verb][context] _stub_path_not_found(path, verb, context) unless spec rescue NoMethodError _stub_path_not_found(path, verb, context) end ## # Raw response from the service. # [HTTP CODE, JSON String] ex.example_group.let(:raw_service_response) { @rester_adapter.request(verb.downcase.to_sym, path, spec['request']) } ## # Parsed service response ex.example_group.let(:service_response) { JSON.parse(raw_service_response.last, symbolize_names: true) } ## # HTTP status code returned by service. ex.example_group.let(:service_response_code) { raw_service_response.first } ## # Expected response body specified in by the stub. ex.example_group.let(:stub_response) { JSON.parse((spec['response'] || {}).to_json, symbolize_names: true) } ## # HTTP status code expected by the stub. ex.example_group.let(:stub_response_code) { spec['response_code'] } ## # Set the subject to be the service response (parsed ruby hash of the # returned data). ex.example_group.let(:subject) { service_response } end
_stub_path_not_found(path, verb, context)
click to toggle source
# File lib/rester/rspec.rb, line 113 def _stub_path_not_found(path, verb, context) fail Rester::Errors::TestError, "Could not find path: #{path.inspect} verb: #{verb.inspect} context: "\ "#{context.inspect} in #{@rester_stub_filepath}" end
_validate_test_coverage(ex)
click to toggle source
Check to see if each stub example has a corresponding test written for it
# File lib/rester/rspec.rb, line 121 def _validate_test_coverage(ex) rester_service_tests = _rester_service_tests(ex) missing_tests = _missing_stub_tests(rester_service_tests) # Loop through each missing stub test and create a corresponding RSpect test # to display the missing tests as a failure to the user missing_tests.each { |missing_path, missing_verbs| path_group = _find_or_create_child(ex.class, missing_path) missing_verbs.each { |missing_verb, missing_contexts| verb_group = _find_or_create_child(path_group, missing_verb) missing_contexts.each { |missing_context, _| _find_or_create_child(verb_group, missing_context).pending } } } end