It works, but it seems sloppy to me. I am wondering if this is a code smell or if there is a better way to achieve this result. The main question is how to drown out some arbitrary object in a ruby.
I am testing a marginal case - the final value of the auxiliary analysis method correctly formats the result of a Google analytics request (thus, an odd statement of approval), the incoming data is a Google Analytics object, the data of which is inside we should call result.data["rows"]
. The whole purpose of this structure is to give the internal device of my method the ability to send this #data message. The test passes / does not work properly, but, as I said, I wonder if this is the best way to do this, for example, to output my data from the GA result object before sending it for analysis.
my approach from the test - in fact, it calls parse_monthly_chart_data(@ga_result)
def test_parse_monthly_chart_data_with_good_values
typical_data = {"rows" => [["0000", "194346"]...more arrays...]}
typical_vals = typical_data["rows"].to_h.values.map(&:to_i)
expected_result = typical_vals[-30..-1].inject(&:+)
Struct.new("GaResult") {def data; end }
@ga_result = Struct::GaResult.new
@ga_result.stub :data, typical_data do
assert_equal(ga.send(:parse_monthly_chart_data, @ga_result).flatten.last, expected_result)
end
end
Edit: I decided to partially solve this problem by replacing the stub with a mocha implementation. I'm still wondering if this is the smell of code.