I am reviewing an ASP.NET Core project created in F# (but not using Giraffe) and wondering how can tests can properly be achieved on controllers.
I purposefully (over) simplified the code of a controller in that source code:
[<Route("api/v1/A")>]
[<ApiController>]
type AController (myDbContext: MyDbContext) =
inherit ControllerBase()
[<HttpPost("post-a")>]
member this.PostA() =
myDbContext.SaveChanges()
this.Ok()
Reading that answer How to mock out rich dependencies when testing F#, it seems that it would be beneficial to leverage the dependency as a simple function parameter which would lead me to think about this sort of solution:
[<Route("api/v1/A")>]
[<ApiController>]
type AController (myDbContext: MyDbContext) =
inherit ControllerBase()
[<HttpPost("post-b")>]
member this.PostB() =
this.PostB (fun () -> myDbContext.SaveChanges() |> ignore)
[<NonAction>]
member this.PostB (whateverDependencyAction: unit -> unit) =
whateverDependencyAction()
this.Ok()
However, not sure leaking details about testing is a good thing either.
Aucun commentaire:
Enregistrer un commentaire