F # - How to correctly implement Setup and initialize variables using NUnit

I am doing my first dive in F # at work, and I am moving a few C # tests that I have for F # as an exercise. Our tests are quite complicated, but I enjoy the problem (with settings, inheritance, breaks, etc.).

As I have already seen, mutation should be avoided if possible, but when writing parts of the tests, [SetUp]I cannot find a way to jump over variability. An example that creates dummy XML for a test:

[<TestFixture>]
type CaseRuleFixture() = 

    [<DefaultValue>] val mutable xsl : XNamespace
    [<DefaultValue>] val mutable simpleStylesheet : XElement
    [<DefaultValue>] val mutable testNode : XElement
    [<DefaultValue>] val mutable rootNode : XElement
    [<DefaultValue>] val mutable root : XElement

    let CreateXsltHeader(xsl: XNamespace) =
        // Build XSLT header
        let styleSheetRoot = 
            new XElement(
                xsl + "stylesheet", 
                new XAttribute(XName.Get "version", "1.0"), 
                new XAttribute(XNamespace.Xmlns + "xsl", "http://www.w3.org/1999/XSL/Transform"), 
                new XAttribute(XNamespace.Xmlns + "msxsl", "urn:schemas-microsoft-com:xslt"),
                new XAttribute(XName.Get "exclude-result-prefixes", "msxsl"),
                new XAttribute(XNamespace.Xmlns + "utils", "urn:myExtension"))

        let outputNode = 
            new XElement(
                xsl + "output", 
                new XAttribute(XName.Get "method", "xml"), 
                new XAttribute(XName.Get "indent", "yes"))

        styleSheetRoot.Add outputNode
        styleSheetRoot


    [<SetUp>]
    member this.SetUp() =
        this.xsl <- XNamespace.Get "http://www.w3.org/1999/XSL/Transform"
        this.simpleStylesheet <- CreateXsltHeader(this.xsl)

        Directory.EnumerateFiles "Templates"
        |> Seq.iter(fun filepath -> this.simpleStylesheet.Add(XElement.Parse(File.ReadAllText filepath).Elements()))

        let variable = 
            new XElement(
                this.xsl + "variable", 
                new XAttribute(XName.Get "name", "ROOT"), 
                new XAttribute(XName.Get "select", "ROOT"))

        this.simpleStylesheet.Add(variable)

        let rootTemplate = new XElement(this.xsl + "template", new XAttribute(XName.Get "match", "/ROOT"))
        this.simpleStylesheet.Add(rootTemplate);

        this.rootNode <- new XElement(XName.Get "ROOT")
        rootTemplate.Add(this.rootNode);

        this.root <- new XElement(XName.Get "ROOT")
        this.testNode <- new XElement(XName.Get "TESTVALUE")

        this.root.Add(this.testNode)

    [<Test>]
    member this.CaseCapitalizeEachWordTest() =
        this.testNode.Value <- " text to replace ";

        let replaceRule = new CaseRule();
        replaceRule.Arguments <- [| "INITIALS" |];
        this.rootNode.Add(
            replaceRule.ApplyRule [| new XElement(this.xsl + "value-of", new XAttribute(XName.Get "select", "TESTVALUE")) |]);

        let parser = new XsltParserHelper(this.simpleStylesheet);
        let result = parser.ParseXslt(this.root);

        let value = result.DescendantsAndSelf() |> Seq.find(fun x -> x.Name = XName.Get "ROOT")

        Assert.AreEqual(" Text To Replace ", value.Value)

[<DefaultValue>] val mutable ( - SetUp) , 1:1 , # - , . , ? , , , .

+4
1

:

, SetUp:

[<TestFixture>]
type MutableTests() = 
    [<DefaultValue>] val mutable foo : int
    [<DefaultValue>] val mutable bar : int

    [<SetUp>]
    member this.SetUp () =
        this.foo <- 42
        this.bar <- 1337

    [<Test>]
    member this.TheTest () = 
        Assert.AreEqual(42, this.foo)
        Assert.AreEqual(1337, this.bar)

, .

,

, , , ?

module BetterTests =
    let createDefaultFoo () = 42
    let createDefaultBar () = 1337

    [<Test>]
    let ``a test using individual creation functions`` () =
        let foo = createDefaultFoo ()
        let bar = createDefaultBar ()

        Assert.AreEqual(42, foo)
        Assert.AreEqual(1337, bar)

(, ), , :

    let createAllDefaultValues () = createDefaultFoo (), createDefaultBar ()

    [<Test>]
    let ``a test using a single creation function`` () =
        let foo, bar = createAllDefaultValues ()

        Assert.AreEqual(42, foo)
        Assert.AreEqual(1337, bar)

int * int, :

    type TestValues = { Foo : int; Bar : int }

    let createDefaultTestValues () = {
        Foo = createDefaultFoo ()
        Bar = createDefaultBar () }

    [<Test>]
    let ``a test using a single creation function that returns a record`` () =
        let defaultValues = createDefaultTestValues ()

        Assert.AreEqual(42, defaultValues.Foo)
        Assert.AreEqual(1337, defaultValues.Bar)

, # F # - .

F #, Pluralsight F #.

+5
source

Source: https://habr.com/ru/post/1606413/


All Articles