Git Product home page Git Product logo

foq's Introduction

foq's People

Contributors

billhally avatar fsgit avatar fsprojectsgit avatar gdziadkiewicz avatar ptrelford avatar robertdavidsmith avatar saul avatar sergey-tihon avatar spiegelsoft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

foq's Issues

Issues asserting on constrained types

Description

any() comparison fails with constrained generics

Repro steps

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'."

Unit type return results in InvalidProgramException

Description

Building a mock for a member that returns a unit type results in a System.InvalidProgramException: "Common Language Runtime detected an invalid program."

Repro steps

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 

Expected behavior

The mock should successfully call foo(5) without error.

Actual behavior

An exception of System.InvalidProgramException is thrown.

Known workarounds

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).

Related information

  • Windows 10 x64
  • For 1.7.2
  • .NET 4.5/4.7 Framework causes this exception. Mono 5.x fails with a stacktrace and the message unknown type 0x01 in type_to_load_membase

.net 4.6.1

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

A mocked abstract base class is "not equal to itself"

Description

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.

Repro

[<AbstractClass>]
type MyAbstractBaseClass() =
    abstract MyMethod : int -> bool
    abstract MyProperty : int

let mock = Mock<MyAbstractBaseClass>().Create()

mock.Equals(mock) // Returns false

Expected behavior

I would expect the mock.Equals(mock) invocation to return true (i.e. for the mock to "be equal to itself")

Actual behavior

The mock.Equals(mock) invocation returns false

Related information

This happens with the current version of the code in this repository.

Please rename `Lib` directory to `lib` in the NuGet package

Description

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.

Repro steps

  1. Create new F# console application.
  2. Install Foq from NuGet.
  3. Try to build the project on Mono.

Expected behavior

Project builds without problems.

Actual behavior

Project fails with the message above.

Known workarounds

I've solved the problem with this commit, simply changing lib to Lib in my fsproj.

Related information

  • Operating system: Windows + Visual Studio when installing the package; Linux + Mono when trying to build the program.
  • Branch: Foq 1.7.2 from NuGet
  • .NET Runtime, CoreCLR or Mono Version: .NET 4.6.2, Mono 5.0.1.1

ReturnFunc not working for int64 properties

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

Release a new version

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

How to mock a function (not a member)?

Description

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

Repro steps

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

Expected behavior

I expected both the asserts to pass.

Actual behavior

A System.NotSupportedException.

Known workarounds

I found none.

Related information

  • Windows, Linux
  • Foq 1.8.0
  • .NET 5

There is a discussion on this topic on StackOverflow's Using Foq With F# Function Types

Any hints?

Is it possible to first get the abstract and then setup its behaviour?

Description

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?

Missing License

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?

[Bug?] Cannot mock class with protected virtual method.

Description

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

Repro steps

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

Expected behaviour

It should jus work, I assume =)
I've discovered this does not work with Moq as well.

Related information

  • Operating system: Win 10 x64
  • Foq v. 1.8.0
  • .NET Runtime: .net core 3.1.101

Support for Xamarin Android

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:

  1. Is Foq a viable option for Xamarin Android?
  • It seems that there are issues/limitations with F# on Mono. Its not clear to me if any of them would be a showstopper.
    1. If so, would a pull request be accepted that added a Xamarin Android build?
  • Foq on Codeplex has some more recent commits, so its not clear to me where the project lives.

Foq error is very unhelpful for internal types

Description

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.

Repro steps

Please provide the steps required to reproduce the problem

  1. Add an internal type to an assembly

  2. Use that internal type as part of an Mock<_>

Expected behavior

Foq should crash with an error explict about the fact an internal type has been used that Foq can't see.

Actual behavior

Foq crashes with a MethodAccessException.

Known workarounds

Just know that MethodAccessException could mean a visibility issue.

Can't mock an abstract class with an "internal set" virtual property

Description

Foq throws an exception if you try to mock a C# abstract class containing a virtual property with an internal setter.

Repro steps

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

Expected behavior

Test should pass.

Actual behavior

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.

Related information

  • Windows 10
  • .NET Core 2.1
  • Foq 1.8.0
  • XUnit 2.4.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.