douxie3625 2019-08-02 20:41
浏览 141

无法在func参数中将bytes.Buffer用作io.WriterAt类型

Struggling with go today.. second question I've had to ask.

I've got 2 tests that test a write function Write(), that takes writer io.WriterAt and content interface{}.

I'm working with (2) tests written for the function, TestWriteSuccessful and TestWriteFail.

The error I'm getting for both functions when testing is:

cannot use &b (type *bytes.Buffer) as type io.WriterAt in argument to Write:

Question

What does implement WriterAt that I can replace bytes.Buffer with in these tests to make the tests functional?

What I've tried

  1. Changing b's type to os.File - the b.len() > 0 fails then.
  2. Extensively googled how to test io.WriterAt and functions implementing it, only thing I found was that bytes.Buffer does NOT implement an io.WriterAt, only io.Writer, but no further information.
  3. Checked the godocs, I see that io.WriterAt calls WriteAt and takes a slice of bytes and an offset: WriteAt(p []byte, off int64) (n int, err error), and I see that bytes.Buffer can be used to call Write, but no information about WriteAt

Code

Write function:

func Write(writer io.WriterAt, content interface{}) error {
    data, err := json.Marshal(content)
    if err != nil {
        return err
    }
    writer.WriteAt(data, 0)
    return nil
}

Tests for Write function (test imports Testify):

func TestWriteSuccessful(t *testing.T) {
    var b bytes.Buffer
    err := Write(&b, exampleSystemConfig)
    assert.Nil(t, err)
    assert.True(t, b.Len() > 0)
}

func TestWriteFail(t *testing.T) {
    var b bytes.Buffer
    err := Write(&b, make(chan int)) // Write will return UnsupportedTypeError
    assert.NotNil(t, err)
}

Expected result

Both tests succeed.

Actual result

internal/platform/store/store_test.go:33:15: cannot use &b (type *bytes.Buffer) as type io.WriterAt in argument to Write:
        *bytes.Buffer does not implement io.WriterAt (missing WriteAt method)
internal/platform/store/store_test.go:40:15: cannot use &b (type *bytes.Buffer) as type io.WriterAt in argument to Write:
        *bytes.Buffer does not implement io.WriterAt (missing WriteAt method)
  • 写回答

1条回答 默认 最新

  • doumowu7371 2019-08-02 20:51
    关注

    It's easy enough to make your own WriterAt for testing purposes — it's any type that implements a WriteAt function with the correct signature. If you're only interested in knowing the number of bytes written, it can be as simple as

    type TestWriter struct {
        BytesWritten int
    }
    
    func (tw *TestWriter) WriteAt(b []byte, _ int64) (n int, err error) {
        tw.BytesWritten += len(b)
        return len(b), nil
    }
    

    then you can test with

    func TestWriteSuccessful(t *testing.T) {
        var tw TestWriter
        err := Write(&tw, exampleSystemConfig)
        assert.Nil(t, err)
        assert.True(t, tw.BytesWritten > 0)
    }
    
    func TestWriteFail(t *testing.T) {
        var tw bytes.Buffer
        err := Write(&tw, make(chan int)) // Write will return UnsupportedTypeError
        assert.NotNil(t, err)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端