English 中文(简体)
F#单位测试中的检查功能平等
原标题:Checking function equality in a F# unit test

我有一套F#功能,对同样的投入采用不同的算法,如《战略》模式。 为了采纳正确的战略,我想根据投入论点来调整,并将这一职能作为价值:

let equalStrategy points : seq<double> =
    ...

let multiplyStrategy fact或 (points: seq<double>) =
    ...

let getStrategy relationship = 
    match relationship with
        | "="  ->  equalStrategy
        | "*5" ->  multiplyStrategy 5.0
        | _    -> raise (new System.NotImplementedException(" relationship not handled"))

Now I want to write some unit tests to make sure that I return the right strategy, so I tried something like this in nUnit :

    [<TestCase("=")>]
    [<Test>]
    member self.getEqualstrategy( relationship:string ) =            
        let strategy = getStrategy relationship

        Assert.AreEqual( strategy, equalStrategy )

现在,我认为该守则是正确的,我将做我想要做的事情,但这一说法失败,因为职能似乎没有为它们规定平等行动。 因此,我的问题是:

(a) is there a way to compare 2 functions to see if they are the same, i.e. let isFoo bar = foo == bar, that I can use in an nUnit assertion?

(b) is there another unit testing framew或k that will do this assertion f或 me in F#?

最佳回答

测试由您的<代码>getStrategy退回的F#功能是否与你定义的幻觉相同,基本上是不可能的。

www.un.org/Depts/DGACM/index_spanish.htm 提供一些细节——F# Editorer产生一个从FSharpFunc继承的类别。 当你恢复一项价值的职能时。 更重要的是,每当你创造职能价值时,它就会产生一个new<>/em>级,因此你不能比较这些类别。

制作的班级结构类似:

class getStrategy@7 : FSharpFunc<IEnumerable<double>, IEnumerable<double>> {
  public override IEnumerable<double> Invoke(IEnumerable<double> points) {
    // Calls the function that you re returning from  getStrategy 
    return Test.equalStrategy(points);
  }
}

// Later - in the body of  getStrategy :
return new getStrategy@7(); // Returns a new instance of the single-purpose class

原则上,您可使用“反思”方法,在方法中找到哪一种功能,但这不是可靠的解决办法。

www.un.org/Depts/DGACM/index_spanish.htm 在实践中,——我认为你或许应当使用一些更简单的测试方法,以检查<代码>getStrategy的功能是否恢复正确的算法。 如果你根据几个样本投入来实施回归战略,那就足以证明返还算法是正确的,而你则不依赖执行细节(例如<代号>植物标准是否仅能恢复一个指定职能,或者它是否恢复新的拉姆布达功能和相同的行为)。

或者,你可以在<代码>Func<_,__>代表中总结职能,并在C#中采用同样的做法。 然而,我认为,检查<代号>植物卫生标准是否回过来,是一种过于详细的检验标准,仅仅限制了你的执行。

问题回答

职能没有平等比较:

您将出现错误: 类型(a->a)不支持平等限制,因为它是一种职能类别

There is a good post here

F#汇编者很难正式证明,两个职能的产出总是相同(相同投入)。 如果有可能,你可以使用F#来证明数学微调。

作为下一步,就纯粹的职能而言,你可以核实,两个职能对大量不同投入拥有相同产出。 http://fscheck.codeplex.com/“rel=“nofollow”>fscheck 能够帮助您使这种测试自动化。 我没有使用过这一条,但我利用了基于同样想法的沥滤(两个是Haskell sQuickCheck的港口)。





相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...