0

I have the following method which is going to execute a lot of raw sql and return it in an array.

I then transform the array to JSON. I want to write a test that checks that this transformation to Json is correct.

def get_data
  results = connection.raw_connection.execute(sql).each
  transform_results(results)
end

private  
def transform_results(results)
      {
          some_value: results[0]['some_value'],
          another_value: results[0]['another_value']
      }
 end

How can I write some Rspec which tests the transform_results method to check the Json is mapped correctly, assuming I mock a results array.

Something like this?

allow_any_instance_of(?????).to receive(:each).and_return(result)

user3437721
  • 2,227
  • 4
  • 31
  • 61

1 Answers1

0

In general, you should not test private methods - just stay with testing public, get_data method.

Here is a stack question: Should I test private methods using RSpec?

However, there is a way to test private methods using send. And here is a stack question about how to test private methods in RSpec.

To test Hashes (which you might find useful to test get_data) you can use include matchers

it 'maps results correctly' do
  expect(instance.send(:transform_results, :results)).to include(some_value: 'some_value', antoher_value: 'antoher_value')
end
nan
  • 63
  • 7