-
-
Save marisks/988bce62583c4fb2a35e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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