Skip to content
This repository has been archived by the owner on Oct 31, 2021. It is now read-only.

replace some poor man mocks with Foq #606

Merged
merged 1 commit into from
Jul 28, 2014

Conversation

vasily-kirichenko
Copy link
Contributor

No description provided.

@vasily-kirichenko
Copy link
Contributor Author

Foq cannot mock methods with byref arguments:

image

So, I'm not sure we should switch to it. However, code has become much more concise.

@dungpa
Copy link
Contributor

dungpa commented Jul 26, 2014

It seems that's possible https://twitter.com/ptrelford/status/314784763015753728. I agree it's better and more flexibile with a mocking framework.

@vasily-kirichenko
Copy link
Contributor Author

The same error:

image

Code

 let documentFactoryService = 
        Mock<ITextDocumentFactoryService>()
            .SetupByName("TryGetTextDocument")
            .Calls<ITextBuffer * byref<ITextDocument>>(fun (textBuffer, textDocument) ->
                    textBuffer.Properties.TryGetProperty(typeof<ITextDocument>, &textDocument))
            .Create()

/cc @ptrelford

@dungpa
Copy link
Contributor

dungpa commented Jul 27, 2014

@ptrelford We are mocking C# classes; most of mocking calls from F# projects but there are some from a C# project. We have a problem with mocking C# calls with byref arguments e.g. https://github.com/fsprojects/VisualFSharpPowerTools/blob/master/src/FSharpVSPowerTools/SyntaxConstructClassifierProvider.cs#L388. Do you have any advice how to fix this? Thanks.

let classificationRegistry =
Mock<IClassificationTypeRegistryService>()
.Setup(fun x -> <@ x.GetClassificationType (any()) @>)
.Calls<string>(fun t -> Mock<IClassificationType>.With(fun x -> <@ x.Classification --> t @>))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this --> thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It means that x.Classification will return t.

@dungpa
Copy link
Contributor

dungpa commented Jul 28, 2014

I'm merging this as an improvement. It would be great to fix the issue with byref arguments to fully migrate to Foq.

dungpa added a commit that referenced this pull request Jul 28, 2014
replace some poor man mocks with Foq
@dungpa dungpa merged commit 4b03df0 into fsprojects-archive:master Jul 28, 2014
@ptrelford
Copy link

I've implemented early support for byref args using Calls in Foq, see changeset:

https://foq.codeplex.com/SourceControl/changeset/9606bc475eca

In your example you should be able to specify:

.SetupByName("TryGetTextDocument")
.Calls<ITextBuffer * ITextDocument>(fun (textBuffer, textDocument) ->

Which should pass the ITextDocument byref argument by value to the specified function.

You can check the functionality by building Foq from source or by copying the latest Foq.fs into your project:

https://foq.codeplex.com/SourceControl/latest#Foq/Foq.fs

It would be great to get your feedback on the implementation before I push out a new Nuget package.

@dungpa
Copy link
Contributor

dungpa commented Aug 12, 2014

It doesn't work; test runners crash whenever I use SetupByName and I have no clue why. You can see my three failed attempts at https://github.com/dungpa/PowerTools/blob/4928dbe03f63637568806aec9e14c1b1b9cd1138/tests/FSharpVSPowerTools.Tests/Mocks.fs.

You could reproduce this by uncommenting relevant sections and running build.cmd UnitTests target. The branch is at https://github.com/dungpa/PowerTools/tree/foq.

@ptrelford
Copy link

@dungpa thanks, I'll take a look

@dungpa
Copy link
Contributor

dungpa commented Sep 8, 2014

@ptrelford Hi Phil, is there any progress?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants