英文:
Go: Test cannot pass?
问题
我在解决这个测试问题时遇到了困难。问题是,我的结构体中的Write()
方法需要写入一个属性,但是io.Writer
接口不接受指针作为其接收器。
filelogger.go:
package logger
import (
"io"
)
type FileLogger struct{
File io.Writer
}
func NewFileLogger(file io.Writer) *FileLogger{
return &FileLogger{file}
}
func (this *FileLogger) Log(message string) error {
_, err := this.File.Write([]byte(appendNewLine(message)))
return err
}
filelogger_test.go:
package logger
import (
"testing"
"github.com/stretchr/testify/assert"
)
type WriterMock struct{
data []byte
}
func (this WriterMock) Write(b []byte) (n int, err error) {
this.data = append(this.data, b ...)
return len(this.data), nil
}
func NewMockedFileLogger() *FileLogger{
writer := WriterMock{}
fileLogger := FileLogger{writer}
return &fileLogger
}
func TestLog(t *testing.T) {
fileLogger := NewMockedFileLogger()
fileLogger.Log("Hello World!")
assert.Equal(t, "Hello World!", string(fileLogger.File.(WriterMock).data))
}
测试输出:
--- FAIL: TestLog (0.00 seconds)
Location: filelogger_test.go:32
Error: Not equal: "Hello World!" (expected)
!= "" (actual)
FAIL
正如我之前提到的,data []byte
没有被写入,因为我不能将函数接收器设置为指针,因为它无法满足io.Writer
的要求。
英文:
I'm having a problem making this test pass. The problem is, the Write()
method in my struct needs to write to a property, but io.Writer
interface does not accept pointer as its receiver.
filelogger.go:
package logger
import (
"io"
)
type FileLogger struct{
File io.Writer
}
func NewFileLogger(file io.Writer) *FileLogger{
return &FileLogger{file}
}
func (this *FileLogger) Log(message string) error {
_, err := this.File.Write([]byte(appendNewLine(message)))
return err
}
filelogger_test.go:
package logger
import (
"testing"
"github.com/stretchr/testify/assert"
)
type WriterMock struct{
data []byte
}
func (this WriterMock) Write(b []byte) (n int, err error) {
this.data = append(this.data, b ...)
return len(this.data), nil
}
func NewMockedFileLogger() *FileLogger{
writer := WriterMock{}
fileLogger := FileLogger{writer}
return &fileLogger
}
func TestLog(t *testing.T) {
fileLogger := NewMockedFileLogger()
fileLogger.Log("Hello World!")
assert.Equal(t, "Hello World!", string(fileLogger.File.(WriterMock).data))
}
Test Output:
--- FAIL: TestLog (0.00 seconds)
Location: filelogger_test.go:32
Error: Not equal: "Hello World!" (expected)
!= "" (actual)
FAIL
As I mentioned, the data []byte
is not being written to, because I cannot set the function receiver as a pointer, as it won't satisfty the io.Writer
.
答案1
得分: 1
如果你继续使用指向WriterMock
的指针,它应该可以工作:
func NewMockedFileLogger() *FileLogger{
writer := &WriterMock{}
fileLogger := FileLogger{writer}
return &fileLogger
}
然后:
assert.Equal(t, "Hello World!", string(fileLogger.File.(*WriterMock).data))
我错过了:
func (this *WriterMock) Write(b []byte) (n int, err error) {
...
}
你需要在指针接收器上调用Write()
,而不是值接收器。由于在Go中一切都是按值传递的,使用值接收器会创建WriterMock
实例的副本。
英文:
If you keep using a pointer to WriterMock
, it should work:
func NewMockedFileLogger() *FileLogger{
writer := &WriterMock{}
fileLogger := FileLogger{writer}
return &fileLogger
}
Then:
assert.Equal(t, "Hello World!", string(fileLogger.File.(*WriterMock).data))
I missed:
func (this *WriterMock) Write(b []byte) (n int, err error) {
...
}
You need to call Write()
on a pointer receiver, not a value receiver.
Since everything is passed by value in Go, using a value receiver would make a copy of the WriterMock
instance.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论