dart,单元测试私有方法 [英] dart, unit testing private methods

查看:197
本文介绍了dart,单元测试私有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当大的库,但只公开了一个非常小的API,使其易于使用和学习新用户。我想保持我的图书馆这种方式,但我也想确保我有尽可能多的单元测试覆盖尽可能,我想能够直接单元测试我的所有类,但只要我能告诉我可以只有单元测试库的公共API。

I have a library which is quite large but only exposes a very tiny API to make it easy to use and learn for new users. I would like to keep my library this way, but I also want to ensure that I have as much unit test coverage as possible, I would like to be able to directly unit test all of my classes but so far as I can tell I can only unit test the public API of the library.

我可以写单元测试来完全测试公共方法,这将有效地间接测试所有的底层私有类,但如果测试失败,可能意味着在私人代码中挖掘很多东西,以找出哪里出了问题,而不是对每个私人类进行单元测试,所以当出现问题时,它会立即显现出错, 。

I can, of course, write unit tests to fully test the public methods which will effectively indirectly test all of the underlying private classes, but if a test fails it could mean a lot of digging around in the private code to find out where something went wrong, rather than having unit tests for each individual private class so when something goes wrong it's immediately apparent what went wrong and where.

有没有设计模式来帮助这种情况,或者为私人dart类和方法编写单元测试的方法?

Is there a design pattern to help with this situation or a way for unit testing to be written for private dart classes and methods?

推荐答案

如果您将私人类移动到同一个应用程序中的单独库中,那么您可以将它们公开并测试它们。然后,您可以将该库导入当前库,而不导出它(因此,您的库的用户仍然无法使用您的其他类,只要他自己不导入该其他库)。

If you move your private classes into a separate library in the same application then you can make them public and test them. You would then import that library in your current library and not export it (so the user of your library still can't use your other classes as long as he doesn't import that other library himself).

这篇关于dart,单元测试私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆