我希望能够将我的应用程序发布的每个写入/读取记录到底层操作系统,并且(如果可能的话)将FS完全替换为仅驻留在内存中的FS.
可能吗?怎么样?也许有一个随时可用的解决方案?
如何在我的测试中填写os.Stdin以获取使用扫描仪从中读取的函数?
我使用以下功能通过扫描仪请求用户命令行输入:
func userInput() error {
scanner := bufio.NewScanner(os.Stdin)
println("What is your name?")
scanner.Scan()
username = scanner.Text()
/* ... */
}
Run Code Online (Sandbox Code Playgroud)
现在我该如何测试这种情况并模拟用户输入?以下示例不起作用.斯坦丁仍然是空的.
func TestUserInput(t *testing.T) {
var file *os.File
file.Write([]byte("Tom"))
os.Stdin = file
err := userInput()
/* ... */
}
Run Code Online (Sandbox Code Playgroud) 我正在努力理解 Go 中的模拟(我正在寻找与 Mockito.spy 相关的东西,相当于 Go 中的 Java)。
假设我在 Go 中有一个带有 5 个方法的接口。但是我要测试的这段代码只引用了两种方法。现在我如何在不实现所有方法的情况下模拟这种依赖关系,即我在源代码中的实际实现实现了 5 个接口方法,但是有没有办法避免在测试文件中实现 5 个方法的虚拟接口实现。以下是我目前的做法,实现5个方法是可以管理的,但是如果接口有20个方法,模拟实现测试文件中的所有方法变得乏味。
type Client struct {}
type ClientStore interface {
func(c *Client) methodOne() error {// some implementation}
func(c *Client) methodTwo() error {// some implementation}
func(c *Client) methodThree() error {// some implementation}
func(c *Client) methodFour() error {// some implementation}
func(c *Client) methodFive() error {// some implementation}
}
Run Code Online (Sandbox Code Playgroud)
func processFeed(c Client) error {
err := c.methodOne()
if(err != null) {
return err …Run Code Online (Sandbox Code Playgroud) go ×3
mocking ×2
command-line ×1
filesystems ×1
gomock ×1
testify ×1
testing ×1
unit-testing ×1
user-input ×1