Coder Social home page Coder Social logo

Comments (6)

evanphx avatar evanphx commented on May 21, 2024
  1. Could you give me a reproduction of a file that mockery generates an empty file for?
  2. 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{} with nil 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.

mpalki1 avatar mpalki1 commented on May 21, 2024
  1. 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)
}
  1. 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.

evanphx avatar evanphx commented on May 21, 2024

Are you sure that error is coming from code mockery generated? It only uses the name ret, never args.

from mockery.

mpalki1 avatar mpalki1 commented on May 21, 2024

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.

evanphx avatar evanphx commented on May 21, 2024

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.

mpalki1 avatar mpalki1 commented on May 21, 2024

Thanks for the info about casting interface types. This conversation was quite helpful.

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.