Presentation: Introducing F#

Alright, you are like me, so you you’ve talked up F# in your office, and folks are interested! You need to introduce F#, and quick! Well, I have got some help for you!

I have a slide deck for you to use, and a YouTube video for ya to share if you aren’t the presenting sort. Feel free to clone the repository and use as you need to.

Mocking without Dependencies in F#

I am not a fan of mocking frameworks. Never have been. However, F# allows for some very simple mocking behavior which pretty much throws away the need for mocking frameworks across the board.

Imagine you have the following:

using System;

namespace TestNamespace
{
    public struct Item 
    {
         public string Name { get; set; }
         public double Price { get; set; }
    }

    public interface IDoStuff
    {
          int DoStuffForRealsies(int a, int b, Item y);
    }

    public class ImportantClassToTest
    { 
        public ImportantClassToTest(IDoStuff constructorInjectedDependency)
        {
             /// etc.
        }
    }
}

Depending on your needs, you might choose to make a mock of that IDoStuff interface for testing purposes. Maybe your standard IDoStuff has some database stuff you don’t want running in simple unit tests, or maybe it involves another installed dependency. If you are working in C#, you might be stuck with Moq, or NSubstitute, but in F#, you get a much nicer model.

module WhyFSharpIsBetterV2123

open Xunit
open SampleTestableClasses

let stubDoStuff = { new IDoStuff with 
                            member this.DoStuffForRealsies(a, b, y) = 
                                4
                        }

[<Fact>]
let ``To Heck with Mocking``() =
    let rd = new ImportantClassToTest(stubDoStuff)
    Assert.Equal(2, rd.MethodThatRequiresDi(5))


A nice quick implementation of the interface without any mocking extra dependencies, or any extra libraries to maintain. Just use your F# compiler, unit test library of choice and go.