Mocking an F# primality tester
The first thing I notice is the somewhat misleading method name IsPrime. I would call it IsFactor, because it only checks if i is a factor of number - not if it is a prime factor.



Secondly I think it is a problem that PrimeFactorsMock.IsPrime has its own implementation of number % i = 0L because you then don't evaluate the base class implementation which could be invalid. 

A solution to that problem could be as shown below. Be aware that I have dropped the IPrimeChecker, because it is unnecessary.

module PrimeFactors = 

    type PrimeFactors() = 

        // I've renamed IsPrime to IsFactor because it is only checking if i is a factor of number not if it is a prime factor
        // You have to tuple the arguments to make it possible to be called from a derived class by base.IsFactor
        abstract member IsFactor: uint64 * uint64 -&gt; bool
        default this.IsFactor (number, i) = number % i = 0UL

        member this.CheckPrimes number i acc =
            match i with
                | _ when i &gt; (number |&gt; float |&gt; sqrt |&gt; uint64)   -&gt; number::acc
                | _ when this.IsFactor (number, i) -&gt; this.CheckPrimes (number / i) i (i::acc)
                | _  -&gt; this.CheckPrimes number (i + 1UL) acc

        member this.Of number =
            if number = 1UL then
                [||]
            else
                this.CheckPrimes number 2UL [] |&gt; List.toArray




module PrimeFactorsMock =
    open PrimeFactors

    type PrimeFactorsMock() =
        inherit PrimeFactors() 

        let mutable stepCount = 0L

        override this.IsFactor (number, i) = 
            stepCount &lt;- stepCount + 1L
            base.IsFactor (number, i) // Her the base class implementation is called

        member this.StepCount number =
            stepCount &lt;- 0L
            this.Of number |&gt; ignore
            stepCount




When it comes to Object Expressions as a mock for testing, I haven't found a valid approach in this case, because you can't extend an OE beyond the type you "derive" from. For instance the below straight forward solution is not an option:

let mock = { 
    new PrimeFactors.PrimeFactors()
    with 
        member this.IsFactor (number, i) =
            stepCount &lt;- stepCount + 1L
            base.IsFactor (number, i)

    // This is not allowed
    let mutable stepCount = 0L
    member this.StepCount number =
        stepCount &lt;- 0L
        this.Of number |&gt; ignore
        stepCount

}




According to Active Patterns for Prime|NoPrime (or is it Factor?) I would use a union type instead defined as follows:

type FactorType =
    | Factors of uint64 * uint64
    | NonFactor


- used in a way like this:

let getFactorType a b = 
            match a % b with
            | r when r = 0UL -&gt; Factors(b, a / b)
            | _ -&gt; NonFactor


- and matched like this:

match getFactorType num i with
| Factors(a, b) -&gt; getPrimes b a (a::acc)
| NonFactor -&gt; getPrimes num (i + 1UL) acc




An entire solution that uses a discriminated union type for factor/nonfactor and minimize the Mock derive could be as follows - not claiming it to be best practice or anything like that.

module PF = 

    // Instead of an active pattern, I use this discriminated union type
    type FactorType =
        | Factors of uint64 * uint64
        | NonFactor

    // Function prototype for functions testing if a number is a factor of another
    // I've changed the domain type from int64 to uint64 
    type FactorChecker = uint64 * uint64 -&gt; FactorType

    type PrimeFactors() = 

        // This function is made static because it has nothing to do with the class itself
        // and because I want to reuse it in the derived Mock class (se below)
        // I renamed it to getFactorType from IsPrime, 
        // because IsPrime is somewhat misleading 
        // as it only checks if b is a factor of a not if it is a prime factor.
        static member getFactorType (a, b) = 
            match a % b with
            | r when r = 0UL -&gt; Factors(b, a / b)
            | _ -&gt; NonFactor

        // This could be static as well but here I made it a member. 
        // It would be nice to declare it protected, but that's not an option in F#
        // In order to let the client provide the function to check for factors, the last argument is a function of type FactorChecker
        member this.getFactors number (factorChecker: FactorChecker) = 
            if number &lt; 2UL then
                []
            else
                // The stop condition is the (uint64 sqrt) of n
                let max n = n |&gt; float |&gt; sqrt |&gt; uint64

                let rec getPrimes num i acc =                    
                    match i with
                    | _ when i &gt; max num -&gt; num::acc
                    | _ -&gt;
                        match factorChecker (num, i) with
                        | Factors(a, b) -&gt; getPrimes b a (a::acc)
                        | NonFactor -&gt; getPrimes num (i + 1UL) acc

                getPrimes number 2UL []

        member this.Of (number: uint64) : uint64 list = this.getFactors number PrimeFactors.getFactorType

module PFMocks = 
    open PF

    // This is a simplified version of PrimeFactorsMock without the IsPrime override
    type PrimeFactorsMock() =
        inherit PrimeFactors() 

        member this.StepCount number =
            let mutable stepCount = 0L
            let facChecker (a, b) = 
                stepCount &lt;- stepCount + 1L
                PrimeFactors.getFactorType (a, b)
            this.getFactors number facChecker |&gt; ignore
            stepCount