Comments (6)
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.
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.
I also want to point out that I have tried dereferencing the pointer (see below)
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.
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.
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.
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)
- Mockery mistakenly imports internal packages when running the 'mockery' command HOT 2
- error: package "context" without types was imported
- mockery built with go 1.22~rc2 fails HOT 6
- Running mockery with docker does not work with go 1.22
- Could not run the latest Mockery Docker image with 1.22 codebase HOT 1
- Investigate if we can use `GODEBUG=gotypealias=1` to get around type alias sadness HOT 2
- Logging warning when mock config is orphaned HOT 1
- Variadic function issues. HOT 10
- Files being created even with `dry-run: true` HOT 1
- Discussion: is interface just for mocking a good idea? HOT 1
- Configuration option `inpackage` is not functioning as expected HOT 3
- Indirect dependency from go-viper includes critical vulnerability CVE-2021-28235
- Using exclude on windows throws an error because of path separator being used, I have raised a PR HOT 1
- Initialize/setup mock via a constructor param HOT 3
- `disable-version-string` not respected in packages config HOT 7
- Unable to generate interface type from another interface HOT 15
- Feature request: pre-commit hooks HOT 8
- Support empty `.mockery.yaml` config HOT 1
- Allow for No Return Value on Generated Mocks
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mockery.