2

I'm fiddling with a new F# project (of which I haven't made many), and I'm aiming to do it in a TDD fashion. So, I'm trying to familiarize myself with using FsUnit, since I have a lot of experience using NUnit in C# projects, and it seems a pretty common framework to use.

My code looks like the following:

module DatabaseReaderTest

open NUnit.Framework
open FsUnit

[<TestFixture>]
type DatabaseReaderTest ()=

    [<Test>]
    member x.ResultsAreReturnedFromDatabase =
        DatabaseReader.result.GetSqlString(1) |> should equal "first"

As far as I can tell, this follows the example on the FsUnit home page (http://fsunit.codeplex.com/), but the compiler tells me that [<Test>] is not a valid attribute on this language element, by which I assume it means member.

Any tips on what I'm doing wrong here?

Thanks!

McMuttons
  • 871
  • 7
  • 22
  • 1
    You might also be interested in checking out Unquote, http://code.google.com/p/unquote/. Then you'd write your test assertion as `test <@ DatabaseReader.result.GetSqlString(1) = "first" @>` and enjoy plain F# boolean assertion syntax with full compiler static type checking and nice step-by-step failure messages which reduce the need to attach the debugger to analyze failure cause. – Stephen Swensen Oct 08 '11 at 16:51

2 Answers2

7

You probably need to use a method rather than a property. Just add a () argument of type unit.

kvb
  • 54,864
  • 2
  • 91
  • 133
4

As a side-note, if you don't need any custom initialization before tests are run and if you're using a recent version of NUnit, then you should be able to use module with let bound functions for your tests.

This gives you a bit more lightweight syntax. Some people also like to use double-tick syntax that makes it possible to write the test name as an ordinary English sentence (which shows nicely in test runner):

module DatabaseReaderTest =

  [<Test>]
  let ``Results are returned from database`` () =
    DatabaseReader.result.GetSqlString(1) |> should equal "first"
Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • @Carsten König: just use object expressions... I don't remember ever needing Moq / Rhino Mocks. – Mauricio Scheffer Oct 08 '11 at 17:01
  • well from time to time you need more than this but thank you anyway (problem is having a extented set of helper-extension-methods for Moq and reinventing the wheel :) ) – Random Dev Oct 08 '11 at 17:29
  • Yeah, I was intending to use the doubletick syntax, but this was really just a proof of concept to figure out FsUnit. Thanks for the tip about the more lightweight version. Object expressions seem like they would get to be a lot of work if mocking something complex, but that falls a bit outside of this discussion. Would love to hear more about that though. Any tips pointing to a thorough comparison of object expressions and mocking libraries? – McMuttons Oct 10 '11 at 12:42