I am writing a test for a method that is doing an SQL query. For this project they need the response to be JSON. My test is to see that the query returns the expected number of responses. I've run into a road block since my object is JSON instead of an array. How do I convert the JSON to an ARRAY?
test "works" do
report = get :method
assert_equal 1, report.count
end
NoMethodError: undefined method `count'...
I've tried adding array_report = ActiveSupport::JSON.decode(report)
but I get the following error message.
TypeError: no implicit conversion of ActionController::TestResponse into String
How can I parse the JSON correctly?
EDIT
def method
sql = "SELECT MIN(departure_date), ch_invoice.invoice_id
FROM ch_invoice
INNER JOIN ch_trip
ON ch_invoice.invoice_id = ch_trip.invoice_id
WHERE departure_date < SYSDATE
AND service_rendered = 0
AND paid = 1
Group By ch_invoice.invoice_id"
report = ActiveRecord::Base.connection.exec_query(sql)
render json: report
end
EDIT 2
Sample JSON.parse code. This is what the query giving me.
{"min(departure_date)"=>"2015-03-01T00:00:00.000Z", "invoice_id"=>"123catfood"}
{"min(departure_date)"=>"2015-01-01T00:00:00.000Z", "invoice_id"=>"123dogfood"}