Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7747881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:38:27+00:00 2026-06-01T10:38:27+00:00

Given i have the IO function: // this can either be IO or some

  • 0

Given i have the IO function:

// this can either be IO or some other side effect 
//that makes the function less pure
printf "HI"

I want to test that IO was called correctly. An imperative solution for testing that IO was called correctly would be to wrap the IO statement in an object, mock the object, pass the object in using dependency injection, and verify the correct method was called with the correct parameters. I wonder if instead of using dependency injection to test F#, a better way would be checking the output of the function (by asserting that the correct value or function is returned) and stub out the IO call; therefore making the function pure again by eliminating the side effect of an IO call.

I am considering wrapping all IO in a special module like so.

let MyPrint print statement = print statement ; statement

so that i can stub out the IO function and assert in my tests that the correct operation occurred like so:

code under test:

let PrintHi = fun(print) -> MyPrint print "HI"
let DoNothing = fun(print) -> ()

let DoIf conditional = 
     if conditional then PrintHi
     else DoNothing

FsUnit:

[<Test>] member test.
     let printStub value = ()

     ``Test Hi Is Printed When TRUE`` ()=
          let testedFunc = DoIf true
          testedFunc(printStub) |> should equal PrintHi(printStub)

Is this a good way to test IO side effects? Is there a better way? Please keep in mind that my goal is to test any IO, not just a print statement.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:38:29+00:00Added an answer on June 1, 2026 at 10:38 am

    Generally speaking, you’ll want to separate pure code from impure (side-effecting) code; and keep code as pure as possible.

    I recommend reading these articles about it, they’re written for other functional languages but the code they use is simple and the concepts are well explained and can be easily applied in F# (and many other languages for that matter):

    • Introduction to QuickCheck (Haskell wiki)
    • How to write a functional program with IO, mutation, and other effects
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have function that formats a date to coerce to given enum DateType{CURRENT,
I have a function that processes a given vector, but may also create such
I have a function that get the current weeknumber from the given date e.g.
if i have a function A,which can apply a certain rule on a given
I have written a function that gets a given number of random records from
I have this timer function, it gives me following exception. Collection was modified; enumeration
given i have the following block of code (function(){ var mb = { abc:function(){
We have a JavaScript function named move which does just windows.location.href = any given
Is there any possibility in the prefix-function of a given pattern to have something
I have a boolean function which is used in the decision-making of many other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.