我正在尝试使用 go 运行单元测试。这些函数在主文件中正常工作。函数如下:
func LoadLexicon(lexiconPath string) (map[string]string, error) {
m := make(map[string]string)
lexiconPath = strings.TrimSuffix(lexiconPath, "\n")
if lexiconPath == "nil" {
m["COME"] = "k V m"
m["WORDS"] = "w 3` d z"
m["MECCA"] = "m E k @"
return m, nil
}
readFile, err := os.Open(lexiconPath)
if err != nil {
fmt.Println(err)
return m, err
}
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
var fileLines []string
for fileScanner.Scan() {
fileLines = append(fileLines, fileScanner.Text())
}
lex_words := make(map[string]string)
for _, line := range fileLines {
temp := strings.Split(line, "\t")
lex_words[strings.ToUpper(temp[0])] = temp[1]
}
return lex_words, err
}
但是当我运行单元测试时,
func TestLoadLexicon(t *testing.T) {
tests := []struct {
n string
want string
}{
{"COME", "k V m"},
{"WORDS", "w 3` d z"},
{"MECCA", "m E k @"},
}
for _, tc := range tests {
if got, _ := LoadLexicon("nil"); got[tc.n] != tc.want {
t.Errorf("got %s, want %s", got[tc.n], tc.want)
}
}
}
我收到这个错误`运行工具:/usr/local/go/bin/go test -timeout 30s -run ^TestLoadLexicon$
go: 在当前目录或任何父目录中找不到 go.mod 文件;查看“去帮助模块”
试运行于 29/08/2022 02:58:53 < `
斯蒂芬大帝
BIG阳
随时随地看视频慕课网APP
相关分类