某处我读过private methods aren't supposed to be tested. Your tests should only care about the real thing - your public API. If your public methods work, then the private ones they call will also work
因此没有必要测试私有方法,但请考虑下面的示例
private
def valid_extension?(file_path)
extension = file_type(file_path)
extension.in?(%w(csv xls xlsx)) ? true : false
end
上面的私有方法返回文件扩展名是否有效,那么如果有人更改我的私有方法怎么办呢
private
def valid_extension?(file_path)
true
end
它总是会给我有效的扩展名
那么,是否也没有必要测试私有方法。
由于我们无法访问类外的私有方法,所以如何使用rspec-rails测试私有方法。
答案 0 :(得分:2)
根据Rspec, Rails: how to test private methods of controllers?,您可以使用
@ your_instance_name.instance_eval {valid_extension}