如何指定私有方法 [英] How to spec a private method

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

问题描述

我有一个带有私有方法的模型,我想用 RSpec 进行规范,
你通常怎么做?你只测试调用私有方法的方法吗?
或者也指定私人的?如果是这样,你怎么做?

I got a model with a private method I'd like to spec with RSpec,
how do you usually do ? Do you only test the method calling the private one ?
or also spec the private one ? if so, how do you do ?

推荐答案

我总是采用这种方法:我想测试我的类公开的公共 API.

如果您有私有方法,则只能从公开给其他类的公共方法中调用它们.因此,如果您测试这些公共方法在所有条件下都按预期工作,那么您还证明了它们使用的私有方法也能正常工作.

If you have private methods, you only call them from the public methods you expose to other classes. Hence, if you test that those public methods work as expected under all conditions, you have also proven that the private methods they use work as well.

我承认我遇到过一些特别复杂的私有方法.在那种极端情况下,你想测试它们,你可以这样做:

I'll admit that I've come across some especially complex private methods. In that extreme case you want to test them, you can do this:

@obj.send(:private_method)

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

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