This post starts with a basic introduction of F# and finally ends up writing a simple unit test for an F# member. For those who don’t know what F# is all about, Its a product from Microsoft research and now part of VS 2010 family. Actually from Wikipedia it is described as follows:
F# (pronounced F Sharp) is a multi-paradigm programming language, targeting the .NET Framework, that encompasses functional programming as well as imperative object-oriented programming disciplines. It is a variant of ML and is largely compatible with the OCaml implementation. F# was initially developed by Don Syme at Microsoft Research but is now being developed at Microsoft Developer Division and is being distributed as a fully supported language in the .NET Framework and Visual Studio as part of Visual Studio 2010.
Now, let’s first start by creating an F# project using the built in VS template:
Once you hit ok, It will end you up with an empty module project like that follows:
Now, first of all what is a module in F# ? From MSDN library it is stated as:
In the context of the F# language, a module is a grouping of F# code, such as values, types, and function values, in an F# program. Grouping code in modules helps keep related code together and helps avoid name conflicts in your program.
But, it looks to me rather a namespace in C#. Next let’s create the first type in F#.
In F# everything is type , if you mark it as interface then use the interface or less class keyword. Here is in the code block, we can also see that the first member is declared using the with keyword followed by set, get which states that its a read/write property. The second member has :unit-> int , it means that it takes no argument and returns an integer value. Now, what if we had an argument, then the declaration would look like:
Moving on to multiple arguments, it needs to be written in a tupled manner:
The question remains it’s an interface but how we can declare class members that has method body and returns real value. To find out more lets consider this:
Here, type declaration is as expected has a class keyword. But for members there is one instance method and one static method. One returns 10 and another returns 20 but first one has arguments too. The declaration is slightly different for implemented or class members where the assigned value acts as a return value and you have to specify the argument(s) via colon format and using commas like:
Generally in C# we do :
That is translated to F# in the following way :
It concludes that you don’t need to use return keyword in F# instead just write the return value.Of course you have to specify this keyword to let F# know that its an instance member. Now, moving further what would happen if I would implement “IMonky” interface to “Foo” class.
Here, for each interface implemented should be wrapped with the following block :
interface <name> with
// your implementation.
Another thing to notice here that how property getter and setter is implemented using the with keyword. Moreover, the assignment with empty parenthesis like member this.Execute() = () marks that the method is returning anything or a void method.
Finally, as we are set with declaring an interface, its members and how it is implemented. Now, lets write an unit test that mocks a member from IMonkey. I will be using JustMock but can be done using other tools like NSubstitute (The new player in town) / Rhino. To start, first we need to add the following lines before the type and following the module declaration.
Its similar to an using keyword in C#. I am skipping the type declaration and steps of adding new test methods into it that can be done in the same way as shown above. Thus, let’s consider the following snippet:
One of the interesting thing here to notice is the variable assignment, where a dynamic variable is constructed using the let keyword which is similar doing the same in C#:
var monkey = Mock.Create<IMonkey>();
var monkey = Mock.Create<IMonkey>();
In line 6 of Mock.Arrange, the fun ignore –> monkey.Echo() is equivalent to () => monkey.Echo() in C# but can’t use the () as it means differently in F# therefore used an anonymous variable instead. Rest is plain old vanilla that is calling the method and asserting the required.
Hope this information was helpful.
Copyright © 2017, Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks or appropriate markings.