gpt4 book ai didi

go - 在 go 中模拟打开…

转载 作者:行者123 更新时间:2023-12-01 22:43:08 25 4
gpt4 key购买 nike

我一直在关注this answer试图模拟开。我完全没有在哪里。
这是我的测试代码:

func (m mockedFS) Open(name string) (file, error) {
if m.reportErrOpen {
return nil, errors.New("Fake failure")
}
mockedFile := mockIORead{}
mockedFile.On("ReadAll", mock.AnythingOfType("[]uint8")).Return(0, fmt.Errorf("error reading"))
mockedFile.On("Read", mock.AnythingOfType("[]byte")).Return(0, errors.New("NON"))
return mockedFile, nil
}

type mockIORead struct {
mock.Mock
reportErr bool // Tells if this mocked FS should return error in our tests
reportSize int64 // Tells what size should Stat() report in our test
}

func (m mockIORead) Read(b []byte) (n int, err error) {
if m.reportErr {
return 0, errors.New("A fake failure")
}
s := "Fear the old blood"
copy(b[:], s)
return 0, nil
}

func (m mockIORead) Close() error {
return nil
}

func (m mockIORead) ReadAt([]byte, int64) (int, error) {
return 0, nil
}

func (m mockIORead) Seek(int64, int) (int64, error) {
return 0, nil
}

func (m mockIORead) Stat() (os.FileInfo, error) {
if m.reportErr {
return nil, os.ErrNotExist
}
return mockedFileInfo{size: m.reportSize}, nil
}

func TestOok(t *testing.T) {
oldFs := fs
// Create and "install" mocked fs:
mfs := &mockedFS{}
fs = mfs
// Make sure fs is restored after this test:
defer func() {
fs = oldFs
}()
mfs.reportErr = false
mfs.reportErrOpen = false
token, err := Ook("fake")
assert.NotNil(t, err)
assert.Equal(t, "Fear the old blood", token)
}
这是正在测试的代码:
func Ook(name string) (string, error) {

_, err := fs.Stat(name)
if err != nil {
return "", nil
}

file, err := fs.Open(name)
if err != nil {
return "", errors.Wrap(err, "Cannot open token file")
}
defer file.Close()

_, err = ioutil.ReadAll(file)
fmt.Print("PING\n")
if err != nil {
return "", errors.Wrap(err, "Could not read token")
}
return "Fear the old blood", nil
//return string(token), nil
}
我到底做错了什么?

最佳答案

第一个错误是您的 mockIORead.Read()返回错误的值。它必须返回读取的字节数(写入 slice 参数的字节数)(例如 copy() 将返回的内容)。
接下来,mockIORead.Read()必须是有状态的! Reader.Read()可能会被调用多次,不能保证传递的 slice 可以容纳您想要返回的所有数据(通过传递的 b slice )。
所以mockIORead必须存储你要返回的数据,并且必须记住到目前为止已经交付了多少,所以下一个Read()通话可以从那里继续。
一个简单的实现是利用 bytes.Buffer :

type mockIORead struct {
mock.Mock
reportErr bool // Tells if this mocked FS should return error in our tests
reportSize int64 // Tells what size should Stat() report in our test

content *bytes.Buffer
}
当返回这样的 mockIORead , 初始化 content与您希望返回的内容:
func (m mockedFS) Open(name string) (file, error) {
if m.reportErrOpen {
return nil, errors.New("Fake failure")
}
mockedFile := mockIORead{
content: bytes.NewBufferString("Fear the old blood"),
}

return mockedFile, nil
}
并且感谢可用的 bytes.Buffer.Read() 方法, mockIORead.Read()实现可以像这样简单:
func (m mockIORead) Read(b []byte) (n int, err error) {
if m.reportErr {
return 0, errors.New("A fake failure")
}
return m.content.Read(b)
}
Ook()函数本身不应该尝试“stat”,因为您没有模拟它(因此调用原始 os.Stat() 可能会为测试中使用的 "fake" 文件名产生错误):
func Ook(name string) (string, error) {
file, err := fs.Open(name)
if err != nil {
return "", errors.Wrap(err, "Cannot open token file")
}
defer file.Close()

token, err := ioutil.ReadAll(file)
fmt.Print("PING\n")
if err != nil {
return "", errors.Wrap(err, "Could not read token")
}
return string(token), nil
}
和测试代码:
func TestOok(t *testing.T) {
oldFs := fs
// Create and "install" mocked fs:
mfs := &mockedFS{}
fs = mfs
// Make sure fs is restored after this test:
defer func() {
fs = oldFs
}()

mfs.reportErr = false
mfs.reportErrOpen = false
token, err := Ook("fake")
assert.Nil(t, err)
assert.Equal(t, "Fear the old blood", token)
}
这会产生一个成功的(“OK”)测试。

关于go - 在 go 中模拟打开…,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63992279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com