英文:
How can calls to Histogram<T> and Counter<T> be verified in unit tests?
问题
我想在单元测试中进行断言,以确认遥测计量器 Histogram<T>
和 Counter<T>
是否被调用。如果我有一个对单元测试代码正在调用的计量器实例的引用,是否有一种方法可以检查它并确认是否调用了 Record
或 Add
方法?
英文:
I'd like to make a assertions in unit tests to confirm that the telemetry meters Histogram<T>
and Counter<T>
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论