F#中的单元测试私有方法 [英] unit test private methods in F#

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

问题描述

假设我们有一堂课

type ThisClassIsComplicated () = 
    let calculateSomething a b =
        a + b 

在这种情况下,calculateSomething是微不足道的,但是如果更复杂的话,可能有必要验证在那里进行的计算是否正确.

In this case calculateSomething is trivial, but if it would be more complicated it may make sense to verify that the calculations done there are correct.

使用单元测试框架来测试私有方法可能很有意义

It might make sense to use a unit testing framework to test that private methods.

我的问题:如何在F#中对私有方法进行单元测试?

一些随机的想法:

此处中选择的答案建议使用 属性,该属性仅适用于 internal方法.

The selected answer here, suggests to use the InternalsVisibleTo attribute which anyway is applicable only to internalmethods.

特定于F#的路由是什么?在F#设计中这样更好吗?

What is the route specific to F# if any? Is this better in a F# design?

let calculateSomething a b = a + b 

type ThisClassIsComplicated () = 
    member this.Calculate a b = calculateSomething a b

通过使用,甚至可以缩小calculateSomething的范围嵌套模块.

Maybe the scope of calculateSomething could be even narrowed down by having a nested module.

推荐答案

如果您觉得代码太复杂而无法从外部进行测试,请使用后一种方法.而且,如果您想测试内部功能,例如

If you feel like your code is too complicated to test it from the outside, use the latter option. And in case you want to test an inner function like

let myComplicatedOperation input = 
    let calculateSomething a b =
        a + b
    calculateSomething (fst input) (snd input)

您总是可以像这样用curring重写它:

you can always rewrite it with currying like this:

let myComplicatedOperation calculateSomething input =
    calculateSomething (fst input) (snd input)

您的问题似乎与F#没有直接关系.测试私有方法的一般方法通常是通过提取一个类(或者,在F#中,您也可以只提取一个let绑定函数).并让您的受测者在该其他类/函数上公开.

Your question does not seem to be directly related to F# though. The general way to test private methods is typically by extracting a class (or, in F#, you can also just extract a let bound function). And making your testee public on that other class / function.

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

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