How to Test C# Functions interactively with F# Interactive
I have a set of static utility methods including unit tests. But I'd like to have a more interactive way to employ a testing -> fixing -> compiling cycle (REPL) like in Lisp or Smalltalk where one can immediately execute code in interactive mode. I tried to use F# Interactive to test these methods directly from within the opened C# project in VS 2010, but I didn't get it to work.
I know that I have to load the assembly (#r
directive), open the namespace and then can call the methods (and inspect t开发者_运维百科he result). But how do I do it within “F# Interactive” in Visual Studio 2010? I know it is possible with the “Immediate” window available in debug mode, but I want to do it within F# Interactive in "design mode", when I'm writing the code.
You need to include the path to your project using the #I
directive then you may load your assembly and use it. I wrote a simple C# console app try this and got this to work.
using System;
namespace ConsoleApplication1
{
public class Program
{
static void Main(string[] args)
{
PrintMessage();
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
Console.WriteLine();
}
public static void PrintMessage()
{
Console.WriteLine("MESSAGE!");
}
}
}
Then in F# interactive:
> #I "full path to debug directory";;
--> Added 'full path to debug directory' to library include path
> #r "ConsoleApplication1.exe";;
--> Referenced 'full path to debug directory\ConsoleApplication1.exe'
> open ConsoleApplication1;;
> Program.PrintMessage();;
MESSAGE!
val it : unit = ()
So it definitely works, you just need to compile your projects first. Just remember to reset your session to release your assembly beforehand.
精彩评论