question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a FakeItEasy.FSharp package

See original GitHub issue

Spun off from https://github.com/FakeItEasy/FakeItEasy/issues/229#issuecomment-31921208

Will include similar extensions to http://trelford.com/blog/post/MoqEx.aspx

More examples:

//C#
A.CallTo(() => comparer.Compare(
    A<string>._,
    A<string>.That.Matches(s => s == "must be equal to this"))).Returns(-1);
//F#
A.CallTo(fun () -> comparer.Compare(ignored(), is((=) "must be equal to this"))).Returns(-1)
//C#
A.CallTo(() => factory.Create()).Returns(A.Fake<IWidget>());
//F#
A.CallTo(fun () -> factory.Create()).Returns(fake())

We should add a FakeItEasy.FSharp project to FakeItEasy.sln and ensure it gets built, packaged and released along with the other projects.

As for testing, an acceptance test project is probably enough. I don’t think unit tests would add much value.

The test project layout and naming convention is a little messy right now. What is under the Integration Tests solution folder right now is generally anything that is not unit tests. Perhaps the test project should be Integration Tests\FakeItEasy.FSharp.Tests.Acceptance.

I’ve no idea what test framework would be suitable for F#. Currently we use NUnit (which we’re considering replacing with xunit.net) and MSpec.

Issue Analytics

  • State:open
  • Created 10 years ago
  • Reactions:1
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
thomaslevesquecommented, Nov 23, 2017

Alternative implementation, a bit more complete, and more idiomatic:

open FakeItEasy
open FakeItEasy.Configuration
open Microsoft.FSharp.Linq.RuntimeHelpers

module FakeItEasyFSharp =

    let private toExpression (quotation : Quotations.Expr<'a>) =
        let body = LeafExpressionConverter.QuotationToExpression quotation
        Expression.Lambda<Func<'a>>(body)

    let private toVoidExpression (quotation : Quotations.Expr) =
        let body = LeafExpressionConverter.QuotationToExpression quotation
        Expression.Lambda<Action>(body)

    let fake<'a>() = A.Fake<'a>()

    let aCallTo (quotation: Quotations.Expr<'a>) = A.CallTo(toExpression quotation)
    let aCallToVoid (quotation: Quotations.Expr) = A.CallTo(toVoidExpression quotation)
    let anyCallTo(fake: Object) = A.CallTo(fake)

    let ignored<'a>() = A<'a>.Ignored
    let is<'a> f = A<'a>.That.Matches(fun x -> f x)
    
    let returns (value:'a) (configuration: IReturnValueConfiguration<'a>) = configuration.Returns(value)
    let throws (ex: Exception) (configuration: IExceptionThrowerConfiguration<'a>) = configuration.Throws(ex)
    let doesNothing (configuration: IDoNothingConfiguration<'a>) = configuration.DoesNothing()
    let invokes (callback: (unit -> unit)) (configuration: ICallbackConfiguration<'a>) = configuration.Invokes(fun () -> callback())

Usage:

open FakeItEasyFSharp
#nowarn "20"

type IFoo =
    abstract Bar : unit -> unit
    abstract Baz : string -> int

type IFooFactory =
    abstract Create : unit -> IFoo

let comparer = A.Fake<IComparer<string>>()
aCallTo <@ comparer.Compare(ignored(), is((=) "foo")) @> |> returns -1
aCallToVoid <@ comparer.ToString() @> |> throws (Exception("oops"))

let factory = A.Fake<IFooFactory>()
let foo = fake()
aCallTo <@ factory.Create() @> |> returns(foo)
anyCallTo foo |> throws (NotImplementedException())
aCallToVoid <@ foo.Bar() @> |> invokes (fun () -> printfn "hello")
0reactions
thomaslevesquecommented, Nov 23, 2017

OK, apparently my approach isn’t perfect yet… A.CallTo(<@ foo.VoidMethod() @>) still picks the overload with Quotations.Expr<'a> (where 'a is unit; F# doesn’t really have the notion of void). So we would need a method with a different name for configuring void methods, which is annoying… In this case, there’s no real point in using methods rather than functions, since the whole point was to allow overloads.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FakeItEasy 7.4.0
This extension turns AutoFixture into an Auto-Mocking Container. The mock instances are created by FakeItEasy. To use it, add the AutoFakeItEasyCustomization to ...
Read more >
FakeItEasy - It's faking amazing
FakeItEasy logo. The easy mocking library ...
Read more >
Faking Return value with F# and FakeItEasy
I would venture a hypothesis that the "Easy" in "FakeItEasy" stands for "Easy Over Simple". The library says it's "easy", and that is ......
Read more >
FSharp: Learning to use FAKE and Paket
It's used for building executable from the source code, restoring packages by reading the package list, copying artifacts to a specific location ...
Read more >
NuGet Gallery | Autofac.Extras.FakeItEasy 7.0.0
Autofac extension supporting generation of FakeItEasy objects. ... NET CLI; Package Manager; PackageReference; Paket CLI ... dotnet add package Autofac.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found