doupao6698 2019-08-30 05:28
浏览 49

为实现接口的对象创建可测试的示例

According to the GoLang document, you can write examples for a piece of code. See https://blog.golang.org/examples for more information.

I have the following method:

// StdOutSink is a 'log.Sink' implementation which logs to 'StdOut'.
type StdOutSink struct {
}

// Write prints a message to 'StdOut'.
func (s StdOutSink) Write(msg message) {
    fMsg := fmtMsg(msg)

    fmt.Printf("%v
", fMsg)
}

And I would like to make a testable example for this, which is done with the following code:

// Ensures the correct implementation of the 'Write' method.
func ExampleWrite() {
    // Arrange.
    s := new(StdOutSink)

    // Act.
    s.Write(createMessage(traceS, "Message"))
    s.Write(createMessage(debugS, "Message"))
    s.Write(createMessage(infoS, "Message"))
    s.Write(createMessage(warnS, "Message"))
    s.Write(createMessage(errorS, "Message"))
    s.Write(createMessage(fatalS, "Message"))

    // Output:
    // [TRACE]: Message
    // [DEBUG]: Message
    // [INFO]: Message
    // [WARNING]: Message
    // [ERROR]: Message
    // [FATAL]: Message
}

// PRIVATE: createMessage returns a 'log.message' with the given severity and text.
func createMessage(s severity, txt string) message {
    msg := new(message)
    msg.severity = s
    msg.text = txt

    return *msg
}

This all works, the tests are being executed, but go-vet is given me the following message:

`ExampleWrite refers to unknown identifier: Write`

How should I name my example method so that it's linked to the function func (s StdOutSink) Write(msg message)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥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做蓝牙接受端
    • ¥15 基于PLC的三轴机械手程序