Comments (2)
The difference is coming from the type of nil you're using. The key is in the error message itself:
what you specified:
1: <nil>
what it received:
1: (*string)(nil)
This is an edge case of the expectation methods. The arguments in the type signature of the expectation methods are interface{}
because of the need to possibly specify mock.Anything
. So your nil pointer is essentially untyped (or more specifically, it's a nil pointer to interface{}
) and you need to cast it into the appropriate type like you said.
(something to do with go's very confused conception of what nil means)
Which is clearly ridiculous; furthermore, there's no indication what the problem might be or how to fix it.
Well, nil becomes a typed value just like anything else in Go, contrasted to other languages where you can compare nil of different types. It's not ridiculous, this is a pretty consistent way to handle types.
Is there any way for Mockery to detect this situation and emit a more useful error message?
Not really, no. This particular message is coming from testify. Ultimately the issue happens because of a combination of shortcomings in the model that mockery mocks use (that being matching arguments to return values... not easy to do correctly), and a lack of "band-aids" we could apply (like making the arguments in the expectation methods strongly typed, which is a feature I've been wanting to add, but haven't yet because most people can work around it just fine).
I will say that the moq-style mocks we're folding into this project is a better model overall IMO, and you would not encounter this problem here. I don't want to drive you away from mockery but I'm just being honest with the current state of things.
from mockery.
The difference is coming from the type of nil you're using.
As I said, I find "the type of nil" to be a very strange phrase; at the very least the original declaration specifies the function takes a pointer-to-string so that should be enough information for the compiler to infer what I meant without me having to specify the cast manually.
In any case, I understand the limitation is in the underlying model and I appreciate you taking the time to explain. Happy to close this now and leave it as a signpost for any others who are similarly confused.
from mockery.
Related Issues (20)
- Add exclude-regex or similar mechanism to ignore certain types HOT 1
- Avoid trying to mock the generated mocks HOT 2
- Infinite recursion under certain configurations on Windows HOT 10
- Mock generation fails if code contains new Go 1.21.0 built-in function "min" HOT 4
- Weird codesign issue with go 1.21.3 build/install on Apple Silicon HOT 5
- Mockery panics when configuration contains a package with a null value and recursive is true HOT 4
- mockery doesn't work with local package HOT 13
- Omitting `.Return()` on an expectation results in a panic and a stack trace rather than a useful error message HOT 7
- Mockery recursion should handle subpackages with their own config files
- Recursive: true appears to no longer work with v2.37 HOT 2
- Filename generated with variable name HOT 2
- Feature Request: Infer Local Package During Generation HOT 1
- Question: MinTimes(), MaxTimes() HOT 1
- pathlib values undefined HOT 3
- Generated Mock Importing Non-Existent 'command-line-arguments' Package HOT 2
- Option to replace a generic type parameter with a specific type HOT 3
- Should 2.39.1 enough to generate mocks for 2.40.1 HOT 7
- Mockery mistakenly imports internal packages when running the 'mockery' command HOT 2
- error: package "context" without types was imported
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.