Comments (6)
- Could you give me a reproduction of a file that mockery generates an empty file for?
- This is expected. You're hitting the weirdness around how golang treats interface{} and nil. Basically, you need to use
m.On("getData").Return([]byte(nil), nil)
in order to create a nil that is typed as []byte. If you pass a bare nil, it will end up is an interface{} withnil
as the embedded type too. mockery could special case for[]byte
in the way you mention, but this is an issue for all types so I'd rather not have mockery hide this issue.
from mockery.
- Here is the code. In my setup the Err object is actually in a different package.
package interfaces
type Err struct {
msg string
code uint64
}
func (e *Err) Error() string {
return e.msg
}
func (e *Err) Code() uint64 {
return e.code
}
type KeyManager interface {
GetKey(string, uint16) ([]byte, *Err)
}
- Thanks for the tip on casting nil to []byte. However, this does not seem to work for custom interfaces that I have defined.
For example if my getData returned an interface (called UserData) then when I say
m.On("getData").Return(interfaces.UserData(nil), nil) //UserData is a custom interface that I have.
then the line in the mock
r0 = args.Get(0).(interfaces.UserData)
panics saying "panic: interface conversion: interface is nil, not interfaces.UserData"
from mockery.
Are you sure that error is coming from code mockery generated? It only uses the name ret
, never args
.
from mockery.
Yes, that error is from the code mockery generated. (I pasted my own mock above, which also has the same issue).
I therefore think it is not a mockery issue.
m.On("getData").Return(interfaces.UserData(nil), nil) does not work and panics.
m.On("getData").Return((*UserDataImpl)(nil), nil) works. UserDataImpl is a struct that implements the UserData interface.
from mockery.
Ah! Yes of course. You have to specify the concrete type because an interface value always references the concrete type, never an interface type. When you do interface.UserData(nil) it checks that a nil type and value can be a UserData and casts it to that interface only in the code. The value passed into the called function is still the nil type and value. The you cast to a concrete type, it emits a new value with type set as you'd want.
from mockery.
Thanks for the info about casting interface types. This conversation was quite helpful.
from mockery.
Related Issues (20)
- 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
- Confusing error message for nil argument in mock expectation HOT 2
- 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
- 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
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.