Introduction
Welcome to the fourth post in this introductory series on functional programming in F#. In this post we will be building on the function composition concepts we worked through in the previous post and we'll be writing our first unit tests in F#. As an added bonus we will also start looking at how we can separate our code into discrete modules.
Getting Started
We're going to use Visual Studio 2019 Community Edition for this post but everything will also work in VSCode + ionide and Jetbrains Rider.
Create a new folder to store your code.
From the Start screen, select F# from the languages dropdown and Library from the Project Type dropdown. Select the Class Library (.NET Core) option and click Next. Name the project DemoCode and the solution MyDemoCode and select your new folder from the selector. Click on Create.
Once the project has been created, right click on the solution and select Add -> New Project. Select F# from the language dropdown and Test from the Project Type dropdown. Select the xUnit Test Project (.NET Core) and click next. Call the project DemoCodeTests and make sure the folder is the same as for the other project. Click on Create.
We need to add/update some NuGet packages. Right click on the solution and choose Manage Nuget Packages for Solution. Update any packages NuGet informs you of and install the FsUnit and FsUnit.Xunit packages to the test project.
Rename Library.fs in the DemoCode project to Customer.fs and rename Tests.fs in the test project to CustomerTests.fs.
Right click on Dependencies in the test project and add a Project Reference to the DemoCode project.
Build the solution.
Now that we have everything set up, let's start with the code.
Namespaces and Modules
Open Customer.fs in the DemoCode project, clear any existing code and type in the following at the top: