问题描述
我的模型中有以下方法
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.
推荐答案
好的测试是关于验证方法的行为,而不是其内部工作.因此,根据发布的示例,您的测试应该检查 #get_performance_data 是否返回了由您的装置定义的 performance_graph_data 的正确值,而不是检查内部循环实现.
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.
如果您的测试在概念上不是说给定输入 X,我的结果应该始终是 Y",那么您可能正在测试错误的东西.与往常一样,这条规则也有例外,但如果没有很好的理由,我不会违反规则.
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.
这篇关于对每个循环使用 Rspec 的单元测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!