# Mocking of type/class with static method in fsharp

**URL:** <https://forums.fsharp.org/t/mocking-of-type-class-with-static-method-in-fsharp/3094>\
**Category:** Uncategorized\
**Created:** [May 8, 2023, 5:54am UTC](https://forums.fsharp.org/t/mocking-of-type-class-with-static-method-in-fsharp/3094 "2023-05-08T05:54:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aman](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@Aman](https://forums.fsharp.org/u/Aman)\
**Post date:** [May 8, 2023, 5:54am UTC](https://forums.fsharp.org/t/mocking-of-type-class-with-static-method-in-fsharp/3094/1 "2023-05-08T05:54:25Z")

</div>

We are trying to mock type/class with static method , but unable to do it in fSharp, Is there any way to mock it? we are getting "\* Type to mock ( TestRepository) must be an interface, a delegate, or a non-sealed, non-static class " error.

Sample Class :

type TestRepository\<'V when 'V :\> Iview\> =  
{ executeQuery: IQueryable\<'T\> -\>Task\<Result\<seq\<'T\>, exn\>\> }  
static member Create\<'V when 'V :\> Iview\> state : TestRepository\<'V\> =  
{  
executeQuery = executeQuery\<'V\> typeof\<'V\>.FullName state  
}

---

<div class="post-metadata">

**Author:** ![ntwilson](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ntwilson/32/984_2.png) [@ntwilson](https://forums.fsharp.org/u/ntwilson)\
**Post date:** [May 8, 2023, 9:29pm UTC](https://forums.fsharp.org/t/mocking-of-type-class-with-static-method-in-fsharp/3094/2 "2023-05-08T21:29:08Z")

</div>

Welcome Aman!  
How exactly are you trying to mock it? It sounds like you’re using some mocking library? I doubt that a mocking library is going to be able to mock a static member on a class. For that matter, it’s probably not going to be able to mock an F# record _at all_ whether there is a static member or not because I believe F# records will be sealed, and the message says it only works for non-sealed classes. You could build a non-sealed class without using a record:

```auto
type TestRepository<'V when 'V :> Iview> (executeQuery : IQueryable<'T> -> Task<Result<seq<'T>, exn>>) =
  member this.ExecuteQuery src = executeQuery src
  static member Create<'V when 'V :> Iview> state = TestRepository (executeQuery<'V> typeof<'V>.FullName state)

```

though you still probably can’t mock the static member with your mocking library.

That said, if in your consumer you use the F# 7 SRTP syntax to refer to the static method:

```auto
let inline consumer<'a when 'a :> TestRepository<_> and 'a : (static member Create : State -> 'a) > =
  let repo = 'a.Create state
  ...

```

then you should be able to use _any_ type that inherits from TestRepository and defines a static member `Create`, so you could probably roll your own mock rather than use a library.

SRTP is considered pretty advanced though, and I haven’t tested the code I wrote so YMMV. You might want to consider just simplifying the process by ditching mocks and using regular function passing and partial application:

```auto
let consumer (queryFactory : State -> (IQueryable<'T> -> Task<Result<seq<'T, exn>>)) = task {
  let execQuery = queryFactory state
  let! results = execQuery ...
}
...

// from prod - partially apply the real `executeQuery` as the factory function, just like your code sample
consumer (executeQuery<'V> typeof<'V>.FullName)
...

// from tests, "mock" a Repository, but without a mocking library
let mockQuery (_:IQueryable<'T>) = task { 
  printfn "Here I mock running a query"
  return Error (exn ("test failure"))
}

consumer (fun _ -> mockQuery)

```
