2

I have following methods in my model

def get_performance_data para, child_para_hash
  performance_graph_data = {}
  child_para_hash.each do |cp|
    performance_graph_data[cp] =  fetch_per_child_para_data(para, cp)
  end
  performance_graph_data  
end


def fetch_per_child_para_data para, child_para
  test_performances.where(network_data_condition(para, child_para)).select("AVG(value)avg_value, activity").group('act_num')
end

I am having problems in understanding how to write test cases for each loop in model methods.

2 Answers 2

3

Good tests are about validating the behavior of a method, not its internal workings. So, based on the posted example, your tests should be checking that #get_performance_data returns the correct value for performance_graph_data as defined by your fixtures, not checking the internal loop implementation.

If your test isn't conceptually saying "Given input X, my result should always be Y" then you're probably testing the wrong thing. As always, there are exceptions to this rule, but I wouldn't break the rule without a very good reason.

Sign up to request clarification or add additional context in comments.

Comments

0

You can return successive values from a stub by passing multiple arguments to and_return, e.g.

mock_object = double('my_object')
mock_object.stub(:foo).and_return(1,2,3)

mock_object.foo # => 1
mock_object.foo # => 2
mock_object.foo # => 3

You could adapt this pattern in your example and stub the values returned from fetch_per_child_para_data to simulate the effect of repeated method calls in a loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.