Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Port of C# equivalent in SO answer by @nikosbaxevanis :- http://stackoverflow.com/a/19954215/11635
module WebApi2Helpers
// Port of answer by @nikosbaxevanis :- http://stackoverflow.com/a/19954215/11635
open System
open Ploeh.AutoFixture
open Ploeh.AutoFixture.Kernel
open Ploeh.AutoFixture.Xunit
open Ploeh.AutoFixture.AutoFoq
open System.Web.Http.Hosting
open System.Web.Http
open System.Net.Http
type HttpRequestMessageCustomization() =
interface ICustomization with
member this.Customize fixture =
fixture.Customize<HttpRequestMessage>( fun c ->
c
.Without( fun x -> x.Content)
.Do( fun (x:HttpRequestMessage) -> x.Properties.[HttpPropertyKeys.HttpConfigurationKey] <- new HttpConfiguration())
:> ISpecimenBuilder ) |> ignore
type ApiControllerSpecification() =
interface IRequestSpecification with
member this.IsSatisfiedBy request =
match request with
| :? Type as requestType -> typeof<ApiController>.IsAssignableFrom requestType
| _ -> false
type ApiControllerFiller() =
interface ISpecimenCommand with
member this.Execute (specimen,context) =
if specimen = null then raise <| ArgumentNullException "specimen"
if context = null then raise <| ArgumentNullException "context"
match specimen with
| :? ApiController as target ->
target.Request <- context.Resolve typeof<HttpRequestMessage> :?> HttpRequestMessage
| _ -> raise <| ArgumentException( "The specimen must be an instance of ApiController.", "specimen")
type ApiControllerCustomization() =
interface ICustomization with
member this.Customize fixture =
fixture.Customizations.Add(
FilteringSpecimenBuilder(
Postprocessor(
MethodInvoker( ModestConstructorQuery()),
ApiControllerFiller()),
ApiControllerSpecification())) |> ignore
type AutoControllerData() =
inherit AutoDataAttribute(
Fixture().Customize(
CompositeCustomization(
HttpRequestMessageCustomization(),
ApiControllerCustomization(),
AutoFoqCustomization())))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.