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 8041943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:23:57+00:00 2026-06-05T04:23:57+00:00

In F# I want to perform unit testing on a function with several levels

  • 0

In F# I want to perform unit testing on a function with several levels of nested functions.
I want to be able to test the nested functions individually as well, but I do not know how I could invoke them.
When debugging, each of these nested functions is invoked as a type of function object, but I don’t know if I can access them at compile time.

I do not want to change the nesting scheme that I am using because it makes the most sense functionally to have them nested this way because there is a de facto “inheritance” of some of the function parameters at each nested level.

Is something like this possible? If not, what is the general procedure for unit testing nested functions? Are they tested individually with extra parameters and then inserted into their nested position afterwords never to be able to be tested again?

Very small example:

let range a b =
    let lower =  ceil a |> int
    let upper =  floor b |> int
    if lower > upper then
        Seq.empty
    else
        seq{ for i in lower..upper -> i}

How could I test that lower or upper are working properly without changing the nested nature of the code?

  • 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-05T04:24:00+00:00Added an answer on June 5, 2026 at 4:24 am

    I would agree with Daniels comment – if the outer function works correctly, you should not need to test any of the inner functions. Inner functions are really an implementation detail that should not be relevant (especially in functional code, where output does not depend on anything else than inputs). In C#, you also don’t test whether for loop or while loop inside your method works correctly.

    If both the inner and the outer functions are too complex, then perhaps it would be better to write the inner function as a separate function anyway.

    That said, you can, of course, mess with the compiled assembly using reflection and invoke the inner function. Inner functions are compiled as classes with constructor that takes the closure (captured values of the outer function) and Invoke method that takes the actual parameters.

    The following trivial example works, though I have not tested it on anything more realistic:

    open NUnit.Framework
    
    // Function with 'inner' that captures the argument 'a' and takes additional 'x'    
    let outer a b = 
      let inner x = x + a + 1
      (inner a) * (inner b)
    
    // Unit tests that use reflection in a hacky way to test 'inner'
    [<TestFixture>]
    module Tests = 
      open System
      open System.Reflection
    
      // Runs the specified compiled function - assumes that 'name' of inner functions
      // is unique in the current assembly (!) and that you can correctly guess what 
      // are the variables captured by the closure (!)
      let run name closure args = 
        // Lots of unchecked assumptions all the way through...
        let typ =
          Assembly.GetExecutingAssembly().GetTypes()  
          |> Seq.find (fun typ -> 
              let at = typ.Name.IndexOf('@')
              (at > 0) && (typ.Name.Substring(0, at) = name) )
        let flags = BindingFlags.Instance ||| BindingFlags.NonPublic
        let ctor = typ.GetConstructors(flags) |> Seq.head
        let f = ctor.Invoke(closure)
        let invoke = f.GetType().GetMethod("Invoke")
        invoke.Invoke(f, args)
    
      /// Test that 'inner 10' returns '14' if inside outer where 'a = 3'
      [<Test>]
      let test () = 
        Assert.AreEqual(run "inner" [| box 3 |] [| box 10 |], 14)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a DLL in C++ and want to perform unit testing of
I am just starting out with unit testing and have a scenario I'm not
I am new to unit testing for web applications I have a function which
I want to perform the above mentioned operation in Milliseconds as the unit. Which
I am using a unit testing framework to test my libraries. I have quite
I want to write a unit test that performs HTTP requests directly (instead of
I want to mock ASP.NET 3.5 behavior in order to unit test my WebControls:
I want perform a easy task in android, which when receiving a call the
I want to perform rubyf action in VIM when I press F5 if the
I want to perform simple validation against multiple fields. Please note these fields are

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.