mercredi 28 octobre 2015

unit testing accuracy of function composition

I'm writing tests for an object that takes in an input, composes some functions together, runs the input through the composed function, and returns the result.

Here's a greatly-simplified set of objects and functions that mirrors my design:

type Result =
| Success of string

let internal add5 x = x + 5

let internal mapResult number =
    Success (number.ToString())

type public InteropGuy internal (add, map) = 
    member this.Add5AndMap number =
        number |> (add >> map)

type InteropGuyFactory() =
    member this.CreateInteropGuy () =
        new InteropGuy(add5, mapResult)

The class is designed to be used for C# interop which explains the structure, but this problem still can apply to any function under test that composes function parameters.

I'm having trouble finding an elegant way to keep the implementation details of the internal functions from creeping in to the test conditions when testing the composing function, or in other words, isolating one link in the chain instead of inspecting the output once the input is piped entirely through. If I simply inspect the output then tests for each function are going to be dependent on downstream functions working properly, and if the one at the end of the chain stops working, all of the tests will fail. The best I've been able to do is stub out a function to return a certain value, then stub out its downstream function, storing the input of the downstream function and then asserting the stored value is equal to the output of the stubbed function:

[<TestClass>]
type InteropGuyTests() = 

    [<TestMethod>]
    member this.``Add5AndMap passes add5 result into map function``() = 

        let add5 _ = 13

        let tempResult = ref 0
        let mapResult result = 
            tempResult := result
            Success "unused result"

        let guy = new InteropGuy(add5, mapResult)

        guy.Add5AndMap 8 |> ignore

        Assert.AreEqual(13, !tempResult)

Is there a better way to do this or is this generally how to test composition in isolation? Design comments also appreciated.

Aucun commentaire:

Enregistrer un commentaire