如何在单元测试中验证对Histogram<T>和Counter<T>的调用?

huangapple go评论80阅读模式
英文:

How can calls to Histogram<T> and Counter<T> be verified in unit tests?

问题

我想在单元测试中进行断言,以确认遥测计量器 Histogram<T>Counter<T> 是否被调用。如果我有一个对单元测试代码正在调用的计量器实例的引用,是否有一种方法可以检查它并确认是否调用了 RecordAdd 方法?

英文:

I'd like to make a assertions in unit tests to confirm that the telemetry meters Histogram&lt;T&gt; and Counter&lt;T&gt; are being called. If I have a reference to the meter instance that the unit test code is calling, is there a way to inspect it and confirm that the Record or Add methods are being called or not?

答案1

得分: 2

你无法直接测试它们,但我认为它们不应该以这种方式进行测试。

您可以创建一个MetricsListener,它可以监听特定的指标,并验证您获得了正确的结果,而不是正确的值被传递。通过这种方式,您可以传递一个具有特定自动生成名称的指标,然后对其进行断言。类似于您测试Activity的方式。

英文:

You can't test them directly, however, I don't think that's the way that they should be tested.

You can create a MetricsListener that can listen to a specific metric and verify that you get the right outcome rather than the right value is passed. With this, you can pass in a Metric with a specific auto-generated name that you can assert against. Similar to the way that you'd test for Activity.

huangapple
  • 本文由 发表于 2023年7月12日 23:40:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定