Skip to content

Instantly share code, notes, and snippets.

@marisks
Forked from bartelink/AutoControllerData.fs
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marisks/988bce62583c4fb2a35e to your computer and use it in GitHub Desktop.
Save marisks/988bce62583c4fb2a35e to your computer and use it in GitHub Desktop.
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