DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

F#: Forcing type to unit for Assert.ShouldThrow in XUnit.NET

Mark Needham user avatar by
Mark Needham
·
Mar. 29, 09 · · News
Like (0)
Save
Tweet
3.32K Views

Join the DZone community and get the full member experience.

Join For Free

I've started playing around with F# again and decided to try and create some unit tests around the examples I'm following from Real World Functional Programming. After reading Matt Podwysocki's blog post about XUnit.NET I decided that would probably be the best framework for me to use.

The example I'm writing tests around is:

let convertDataRow(str:string) =
    let cells = List.of_seq(str.Split([|','|]))
    match cells with 
    | label::value::_ -> 
        let numericValue = Int32.Parse(value)
        (label, numericValue)
    | _ -> failwith "Incorrect data format!"

I started driving that out from scratch but ran into a problem trying to assert the error case when an invalid data format is passed in.

The method to use for the assertion is 'Assert.ShouldThrow' which takes in an Assert.ThrowsDelegate which takes in an argument of type unit->unit.

The code that I really want to write is this:

[<Fact>]
let should_throw_exception_given_invalid_data () =
    let methodCall = convertDataRow "blah"
    Assert.Throws<FailureException>(Assert.ThrowsDelegate(methodCall))

which doesn't compile giving the error 'This expression has type string*int but is used here with type unit->unit'.

I got around the first unit by wrapping the convertDateRow in a function which takes in no arguments but the output was proving tricky. I realised that putting a call to printfn would solve that problem, leaving me with this truly hacky solution:

[<Fact>]
let should_throw_exception_given_invalid_data () =
    let methodCall = fun () -> (convertDataRow "blah";printfn "")
    Assert.Throws<FailureException>(Assert.ThrowsDelegate(methodCall))

Truly horrible and luckily there is a way to not do that printfn which I came across on the hubfs forum:

[<Fact>]
let should_throw_exception_given_invalid_data () =
    let methodCall = (fun () -> convertDataRow "blah" |> ignore)
    Assert.Throws<FailureException>(Assert.ThrowsDelegate(methodCall))

The ignore function provides a neat way of ignoring the passed value i.e. it throws away the result of computations.

 

unit test Framework Data (computing) POST (HTTP) Assertion (software development) Scratch (programming language) Blog

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Modernizing Testing With Data Pipelines
  • How to Design a CRUD Web Service for Inheritable Entity
  • Java: Why Core-to-Core Latency Matters
  • Comparing Distributed Databases

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo