我第一次尝试全神贯注地编写一些单元测试,并且我正在 golang 中为使用 aws lambda 的辅助项目做这件事。
下面是两个文件。
main.go
接受一个包含电子邮件地址的事件,并在认知用户池中创建用户。
main_test.go
应该模拟中的createUser
函数main.go
,但是当我尝试运行测试时出现错误。
观看此 youtube 视频后,我刚刚将我的代码从全局实例化客户端切换为在 aws sdk 接口上使用指针接收器方法。
错误
Running tool: /usr/local/go/bin/go test -timeout 30s -run ^TestCreateUser$ github.com/sean/repo/src/create_user
--- FAIL: TestCreateUser (0.00s)
--- FAIL: TestCreateUser/Successfully_create_user (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x60 pc=0x137c2f2]
goroutine 6 [running]:
testing.tRunner.func1.1(0x13e00c0, 0x173fd70)
/usr/local/go/src/testing/testing.go:1072 +0x30d
testing.tRunner.func1(0xc000001b00)
/usr/local/go/src/testing/testing.go:1075 +0x41a
panic(0x13e00c0, 0x173fd70)
/usr/local/go/src/runtime/panic.go:969 +0x1b9
github.com/sean/repo/src/create_user.(*mockCreateUser).AdminCreateUser(0xc00000ee40, 0xc00006a200, 0xc00001e39d, 0x18, 0xc0000b24b0)
<autogenerated>:1 +0x32
github.com/sean/repo/src/create_user.(*awsService).createUser(0xc000030738, 0x145bbca, 0x10, 0x18, 0x0)
/Users/sean/code/github/sean/repo/src/create_user/main.go:39 +0x2b7
github.com/sean/repo/src/create_user.TestCreateUser.func1(0xc000001b00)
/Users/sean/code/github/sean/repo/src/create_user/main_test.go:30 +0x10c
testing.tRunner(0xc000001b00, 0x1476718)
/usr/local/go/src/testing/testing.go:1123 +0xef
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:1168 +0x2b3
FAIL github.com/sean/repo/src/create_user 0.543s
FAIL
白衣非少年
相关分类