A unit testing framework for F#
See http://www.slideshare.net/ptrelford/foq-17062247
The default maintainer account for projects under "fsprojects" is @fsprojectsgit - F# Community Project Incubation Space (repo management)
A unit testing framework for F#
Home Page: http://www.slideshare.net/ptrelford/foq-17062247
License: Apache License 2.0
A unit testing framework for F#
See http://www.slideshare.net/ptrelford/foq-17062247
The default maintainer account for projects under "fsprojects" is @fsprojectsgit - F# Community Project Incubation Space (repo management)
any() comparison fails with constrained generics
I am trying to set up a method on my mock EntityFramework context:
member this.UpdateExisting<'a when 'a: not struct> (existing, updated) = base.Entry<'a>(existing).CurrentValues.SetValues(updated)
my mocked context is set up as so ( I have to use Moq for the DbSet as Foq's type checking is too strict) :
let dtos : Moq.Mock<DbSet<SurveyDto>> = [] |> Helpers.getQueryableMockDbSet
let dbContext = Mock<IDatabaseContext>() .Setup(fun ctx -> <@ ctx.Surveys @>) .Returns(dtos.Object) .Create()
I try to assert like this, including a type:
verify <@dbContext.Add<SurveyDto>(any())@> once
or like this without one:
verify <@dbContext.Add(any())@> once
Either way I get a failure with
"System.TypeLoadException : GenericArguments[0], 'a', on 'Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry`1[TEntity]' violates the constraint of type parameter 'TEntity'."
Based on SO question: Mocking a class with an explicitly implemented interface using Foq it would be nice to be able to Mock "As" another interface in a similar fashion to Moq.
This has been implemented in the Mercurial repository on CodePlex.
Building a mock for a member that returns a unit type results in a System.InvalidProgramException: "Common Language Runtime detected an invalid program."
Compile and run the following program:
open Foq
type IMyInterface =
abstract member foo: int -> unit
let mockMyInterface = Mock<IMyInterface>.With(fun mock ->
<@ mock.foo(any()) --> () @>
)
[<EntryPoint>]
let main args =
mockMyInterface.foo(5)
0
The mock should successfully call foo(5) without error.
An exception of System.InvalidProgramException is thrown.
It seems if I build the mock out via the Setup/Create approach this works correctly:
let mockMyInterface =
Mock<IMyInterface>()
.Setup(fun x -> <@ x.foo() @>)
.Returns(())
.Create()
The documentation is sparse, so I'm unsure of the difference between the two (I opted for the .With() for brevity).
unknown type 0x01 in type_to_load_membase
Iām just trying to upgrade a unit test project to .net 4.6.1 - Iām getting some very odd messages when trying to use Foq:
System.MissingMethodException : Method not found: 'Void Foq.Mock`1..ctor(Foq.MockMode, Microsoft.FSharp.Core.FSharpOption`1<Microsoft.FSharp.Core.FSharpFunc`2<System.Type,System.Object>>)'.
Before I go digging to much I just wanted to know if Foq is compatible with frameworks above v4.5
When mocking an abstract base class, the resultant mock has an .Equals(_:obj) method which returns false when invoked with the mock itself as its argument.
[<AbstractClass>]
type MyAbstractBaseClass() =
abstract MyMethod : int -> bool
abstract MyProperty : int
let mock = Mock<MyAbstractBaseClass>().Create()
mock.Equals(mock) // Returns false
I would expect the mock.Equals(mock)
invocation to return true (i.e. for the mock to "be equal to itself")
The mock.Equals(mock)
invocation returns false
This happens with the current version of the code in this repository.
I had the following problem when installing Foq on Mono: it has successfully been downloaded by NuGet:
Added file 'Foq.dll' to folder '/home/travis/build/ForNeVeR/1969/packages/Foq.1.7.2/Lib/net40'.
Added file 'Foq.XML' to folder '/home/travis/build/ForNeVeR/1969/packages/Foq.1.7.2/Lib/net40'.
Added file 'Foq.dll' to folder '/home/travis/build/ForNeVeR/1969/packages/Foq.1.7.2/Lib/net45'.
Added file 'Foq.XML' to folder '/home/travis/build/ForNeVeR/1969/packages/Foq.1.7.2/Lib/net45'.
Added file 'install.ps1' to folder '/home/travis/build/ForNeVeR/1969/packages/Foq.1.7.2/tools'.
But the compiler still complains it can't found Foq.dll
:
Target ResolveAssemblyReferences:
/usr/lib/mono/xbuild/14.0/bin/Microsoft.Common.targets: warning : Reference 'Foq' not resolved
For searchpath {CandidateAssemblyFiles}
Warning: {CandidateAssemblyFiles} not supported currently
For searchpath {HintPathFromItem}
Considered ../packages/Foq.1.7.2/lib/net45/Foq.dll, but it does not exist.
Project builds without problems.
Project fails with the message above.
I've solved the problem with this commit, simply changing lib
to Lib
in my fsproj
.
Visual F# 4.4, .net 4.5.2, Foq 1.7.1, debug build
open Foq
type IFoo =
abstract N : int64
[<EntryPoint>]
let main _ =
let mock1 = Mock<IFoo>().Setup(fun foo -> <@ foo.N @>).Returns(7L).Create()
printfn "%d" mock1.N
let mock2 = Mock<IFoo>().Setup(fun foo -> <@ foo.N @>).ReturnsFunc(fun () -> 7L).Create()
printfn "%d" mock2.N
0
Should write
7
7
Actually writes
7
200113899394039815
It'd be really useful if the changes merged in with #8 could be released as a new version on NuGet. At the minute internally we're passing around Foq.fs (from master) and using that instead of the official NuGet source - it's a bit hacky.
Please can a new version be released on NuGet, it'd make our lives so much easier!
Thanks
While mocking a type's member is super convenient, I found no way to mock a bare function, not hosted in any type. Or, at least, I was able to mock it, but assertions seem fail.
Replacing a function with a test double requires no library: it is just a matter of defining the replacing function and using it instead of the original one.
My goal, though, is then verify if the function was called and with which arguments.
When I try, I get a
I can easiliy replace a method with:
type MyInterface =
abstract CallMe: string : string -> string
[<Fact>]
let ``mock a method``() =
let mock = Mock.Of<MyInterface>()
verify <@ mock.CallMe "Hello" @> never
mock.CallMe "Hello"
verify <@ mock.CallMe "Hello" @> once
If I try to do the same with a function:
type MyFunc = string -> string
[<Fact>]
let ``mock a naked function``() =
let mock = Mock.Of<MyFunc>()
verify <@ (mock "Hello") @> never
mock "Hello"
verify <@ (mock "Hello") @> once
I get a:
System.NotSupportedException
Expected function application: ValueWithName (<fun:FSharpFunc`257856397-a910-4815-8fa8-bb79a503eb1e>, mock)
at Foq.Reflection.unwrap@736(FSharpFunc`2 toArgs', FSharpList`1 values, FSharpExpr quote)
at Foq.Reflection.unwrap@736-1.Invoke(FSharpList`1 values, FSharpExpr quote)
at Foq.Mock.DoVerify(FSharpExpr expr, Times expectedTimes)
at SAI.AbsenceCollector.Test.Core.mock a naked function() in C:\Core.fs:line 35
The failure occurs at
verify <@ (mock "Hello") @> never
I expected both the asserts to pass.
A System.NotSupportedException
.
I found none.
There is a discussion on this topic on StackOverflow's Using Foq With F# Function Types
Any hints?
When using Moq
framework I usually create mocks and pass mocked objects to constructor of the object under test.
Doing this I avoid repeating part of initialising logic:
public sealed class FooTests
{
private readonly IMock<IBar1> bar1Mock = new Mock<IBar1>();
private readonly IMock<IBar2> bar2Mock = new Mock<IBar2>();
private readonly Foo sut;
public FooTests()
{
sut = new Foo(bar1Mock.Object, bar2Mock.Object);
}
[Fact]
public void Fact1()
{
// setup mocks here
}
}
Is same achievable with Foq?
Hi, this project doesn't seem to have any license attached, which makes it exclusive copyright by default. Could you please attach a license if that was not the intention?
I want to mock behaviour of HttpMessageHandler
, but cannot do it because of exceptions thrown by Foq.
Is Strict
mode I get NotImplementedException
and in Loose
mode I get NullReferenceException
open System
open System.Net.Http
open Foq
open System.Threading
open System.Threading.Tasks
open System.Reflection
open System.Linq
[<AbstractClass>]
type TestHandler() =
inherit HttpMessageHandler()
abstract member MockableSendAsync: HttpRequestMessage * CancellationToken -> Task<HttpResponseMessage>
override this.SendAsync(request, cancellationToken) = this.MockableSendAsync(request, cancellationToken)
type TestHandler2() =
inherit TestHandler()
override this.MockableSendAsync(request, token) = Task.FromResult(null)
let handler =
Mock<TestHandler2>(MockMode.Strict)
.Setup(fun h -> <@ h.MockableSendAsync(any(), any()) @>)
.Returns(Task.FromResult(null))
.Create()
let method =
typeof<TestHandler2>
.GetMethods(BindingFlags.NonPublic ||| BindingFlags.Instance ||| BindingFlags.Public)
.Single(fun x -> x.Name = "SendAsync")
let p = [|null; box CancellationToken.None|]
let result = method.Invoke(handler, p) :?> Task<HttpResponseMessage>
result.Result
It should jus work, I assume =)
I've discovered this does not work with Moq as well.
Currently, there are few options for mocking/faking libraries that can run on Xamarin Android. Most libraries like moq and fakeiteasy rely on Castle.Core, which does not have a build for Xamarin Android. Since Foq does not have this dependency, it seems like it has a good chance to work for that platform
I hacked up something quick, and was able to get Foq and the C# tests to run on Xamarin. The issues I encountered were that LinqTests.TestSetupFuncWithReturnsLambda failed with an exception related to bad IL code (seems to be a Mono problem) and Microsoft.FSharp.Control does not seem to be available for Xamarin. I encountered a few more failures running the F# tests on Mono too.
This brings me to my questions:
When using an internal type in a mock Foq can crash with incredibly confusing System.MethodAccessException errors.
This can be fixed by either making the type public or adding InternalsVisbleTo("Foq.Dynamic"). But this bug is not that an error occurs but that the error is unhelpful for working out what's actually wrong.
Please provide the steps required to reproduce the problem
Add an internal type to an assembly
Use that internal type as part of an Mock<_>
Foq should crash with an error explict about the fact an internal type has been used that Foq can't see.
Foq crashes with a MethodAccessException.
Just know that MethodAccessException could mean a visibility issue.
Foq throws an exception if you try to mock a C# abstract class containing a virtual property with an internal setter.
Create the following class in a C# project:
public abstract class AbstractClass
{
public int ConcreteInternalSet { get; internal set; }
public virtual int VirtualPrivateSet { get; private set; }
public virtual int VirtualProtectedSet { get; protected set; }
public virtual int VirtualInternalSet { get; internal set; }
public abstract int AbstractReadOnly { get; }
}
Create this test in an F# test project:
[<Fact>]
let abstractClassTest() =
Mock<AbstractClass>().Create() |> ignore
Test should pass.
Fails with this error:
System.TypeLoadException : Method 'set_VirtualInternalSet' on type 'Mock.AbstractClassefa0bfaa-f64d-4c8b-b1b2-7eef47cfe38a' from assembly 'Foq.Dynamic, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is overriding a method that is not visible from that assembly.
Stack Trace:
at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, Int32 tk, ObjectHandleOnStack type)
at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
at System.Reflection.Emit.TypeBuilder.CreateTypeInfo()
at Foq.Emit.mock(MockMode mode, Type abstractType, FSharpList`1 otherTypes, FSharpList`1 calls, Object[] args, FSharpOption`1 returnStrategy)
at Foq.Mock`1.Create()
If you remove the VirtualInternalSet
property, the test passes.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ššš
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ā¤ļø Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.