I want to implement Go interfaces with reflection to generate mocks and stubs. But if I look at the reflect package, I get no idea how to do it (maybe it is not possible).
Example: Testing that a func calls WriteHeader(404)
on ResponseWriter:
type ResponseWriterMock struct { //
status int //
} // How to replace
func (*ResponseWriterMock) Header() Header {} // this block with
func (*ResponseWriterMock) Write([]byte) (i int, e error) {} // a reflectivly
func (m *ResponseWriterMock) WriteHeader(status int) { // generated mock?
m.status = status //
} //
responseWriterMock := new(ResponseWriterMock)
funcToTest(responseWriterMock)
if responseWriterMock.status != 404 {
// report error
}
With RhinoMocks (C#) I would write it like this:
var responseWriterMock = MockRepository.GenerateMock<ResponseWriter>();
funcToTest(responseWriterMock);
responseWriterMock.AssertWasCalled(rw => rw.WriteHeader(404));
How can I implement Go interfaces with reflection?
Addendum