Skip to content

Instantly share code, notes, and snippets.

@bartelink
Created March 21, 2014 22:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bartelink/9697979 to your computer and use it in GitHub Desktop.
Save bartelink/9697979 to your computer and use it in GitHub Desktop.
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