Code Monkey home page Code Monkey logo

Comments (6)

LandonTClipp avatar LandonTClipp commented on July 22, 2024 1

Ah apologies! It seems like the docs are indeed wrong, my bad. I will fix that.

As far as the circular import issue, this is a classic shortcoming of placing the mocks in a separate directory. This is an inherent circular dependency that can only be resolved by placing args.Flags in another package (to break the circular dep) or by placing the mocks in the same package as the interface. The mocks need access to _pathreplaced_/args.Flags but _pathreplaced_/args imports that mock, so what do you do?

https://vektra.github.io/mockery/latest/configuration/#__tabbed_1_2

from mockery.

CoderCoco avatar CoderCoco commented on July 22, 2024 1

Ok yeah I must have missed that tab while going over this, still a bit of a go newby lol.

Got it all up and running now.

Thanks for the help.

from mockery.

CoderCoco avatar CoderCoco commented on July 22, 2024

I also want to point out that I have tried dereferencing the pointer (see below)

image

But I don't think there's a way for this to be done by value instead of reference. When executing my test I always receive:

// Attempt 1
var f = testMock.command.Command.Flags()
(&f).EXPECT().BoolP(flagName, "", false, "").Return(new(bool))

flag := NewFlag[bool](flagName, testMock.command)

// Attempt 2
testMock.flags.EXPECT().BoolP(flagName, "", false, "").Return(new(bool))
flag := NewFlag[bool](flagName, testMock.command)

Both result in the following error

assert: mock: I don't know what to return because the method call was unexpected.
        	Either do Mock.On("BoolP").Return(...) first, or remove the BoolP() call.

from mockery.

LandonTClipp avatar LandonTClipp commented on July 22, 2024

Hi, the config you've specified is not what you want.

with-expecter: True
inpackage: True
dir: mocks/{{ replaceAll .InterfaceDirRelative "internal" "internal_" }}
mockname: "{{.InterfaceName}}"
outpkg: "{{.PackageName}}"
filename: "{{.InterfaceName}}.go"
all: true

You've specified inpackage: True which tells mockery that you're intending to place the mocks in the same location as the original interface. However, your dir parameter has placed it in a separate location. Therefore, mockery is not importing your original package as you expect because of inpackage: True. So in this case, it references Flags as the return value, which it intends to be the original interface, but instead it accidentally resolves to the Flag mock you've created.

from mockery.

CoderCoco avatar CoderCoco commented on July 22, 2024

Ah I see the issue:

I had gotten this config from this page of the documentation: https://vektra.github.io/mockery/latest/migrating_to_packages/#examples

Separate mocks/ directory[ΒΆ](https://vektra.github.io/mockery/latest/migrating_to_packages/#separate-mocks-directory)
Take for example a configuration where you are specifying all: true at the top of your repo and you're placing your mocks in a separate mocks/ directory, mirroring the directory structure of your original repo.

YAML

testonly: False
with-expecter: True
keeptree: True
all: True
The equivalent config for packages looks like this:

YAML

with-expecter: True
inpackage: True
dir: mocks/{{ replaceAll .InterfaceDirRelative "internal" "internal_" }} 
mockname: "{{.InterfaceName}}"
outpkg: "{{.PackageName}}"
filename: "{{.InterfaceName}}.go"
all: True
packages:
  github.com/org/repo:
    config:
      recursive: True
While the example config provided here is more verbose, that is because we're specifying many non-default values in order to retain strict equivalence for this example. It's recommended to refer to the [configuration parameters](https://vektra.github.io/mockery/latest/configuration/#parameter-descriptions) to see the defaults provided.

If this example configuration leads to a bug, at the very least I think the documentation should be changed?

from mockery.

CoderCoco avatar CoderCoco commented on July 22, 2024

I have changed the config to the following:

with-expecter: True
dir: mocks/{{ replaceAll .InterfaceDirRelative "internal" "internal_" }}
mockname: "{{.InterfaceName}}"
outpkg: "{{.PackageName}}"
filename: "{{.InterfaceName}}.go"
all: true
packages:
  "_pathreplaced_/args":
      config:
        all: false
      interfaces:
        Command:
        Flags:

Which now generates the mock with the correct values, however when I go to run the test I now have a circular dependency because of this structure:

package _path_replaced_/args
	imports _path_replaced_/mocks/args
	imports _path_replaced_/args: import cycle not allowed in test

How would I solve this issue, I tried using replace-type like this

with-expecter: True
dir: mocks/{{ replaceAll .InterfaceDirRelative "internal" "internal_" }}
mockname: "{{.InterfaceName}}"
outpkg: "{{.PackageName}}"
filename: "{{.InterfaceName}}.go"
all: true
packages:
  "_pathreplaced_/args":
      config:
        all: false
      interfaces:
        Command:
           config:
             replace-type:
               - _pathreplaced_/args.Flags=_pathreplaced_/mocks/args.Flags
        Flags:

But that leads to a self import in the mocks package.

Not really sure how to move forward from here. Any suggestions?

from mockery.

Related Issues (20)

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.