unit-testing - 错误断言包含且等于未按预期工作
问题描述
我正在断言一个函数的错误,我已经为该函数提供了这样的输入,以便它会抛出一个错误。我想检查错误是否正确。
这是我的功能:
func validateK8ResourcesLength(resource prm.Resource) error {
name := resource.GetName()
namespace := resource.GetNamespace()
k8sResourceName := fmt.Sprintf("%s-%s", namespace, name)
if len(k8sResourceName) > common.K8sNameLength {
return fmt.Errorf("Namespace (%s) and Name (%s) length combined must not be greater than 48 chars", namespace, name)
}
return nil
}
我的测试代码:
func TestK8testing(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockContext := prmmock.NewMockStepContext(ctrl)
mockResource := prmmock.NewMockResource(ctrl)
mockClient := prmmock.NewMockClient(ctrl)
// testClient := fake.NewFakeClientWithScheme(runtime.NewScheme())
lables := make(map[string]string)
mockClient.EXPECT().GetResource(gomock.Any(), gomock.Any()).Return(mockResource, nil).AnyTimes()
mockContext.EXPECT().Resource().Return(mockResource).AnyTimes()
mockResource.EXPECT().GetKind().Return("data-movement-v1-nonexist").AnyTimes()
mockResource.EXPECT().GetName().Return("test-dm-more-then-forty-eight-charactors-this-should-fail").AnyTimes()
mockResource.EXPECT().GetNamespace().Return("ns-more-then-forty-eight-charactors-this-should-fail").AnyTimes()
mockResource.EXPECT().GetLabels().Return(lables).AnyTimes()
mockResource.EXPECT().GetID().Return(prm.NewResourceID("hcc-dm", "data-movement-v1", "test-dm")).AnyTimes()
err := validateK8ResourcesLength(mockResource)
assert.Error(t, err)
if assert.Error(t, err) {
//assert.Contains(t, err, "length combined must not be greater")
assert.Equal(t, "Namespace (ns-more-then-forty-eight-charactors-this-should-fail) and Name (test-dm-more-then-forty-eight-charactors-this-should-fail) length combined must not be greater than 48 chars", err)
}
}
这是我得到的错误:
--- FAIL: TestK8testing (0.00s)
/Users/ngupta59/hcc-dm/pkg/dm-agent/cmd/action_validate_dmjob_test.go:296:
Error Trace: action_validate_dmjob_test.go:296
Error: Not equal:
expected: string("Namespace (ns-more-then-forty-eight-charactors-this-should-fail) and Name (test-dm-more-then-forty-eight-charactors-this-should-fail) length combined must not be greater than 48 chars")
actual : *errors.errorString(&errors.errorString{s:"Namespace (ns-more-then-forty-eight-charactors-this-should-fail) and Name (test-dm-more-then-forty-eight-charactors-this-should-fail) length combined must not be greater than 48 chars"})
Test: TestK8testing
我的代码有什么问题?我assert.contains
的也不工作。
解决方案
错误内置类型是一个接口:
type error interface {
Error() string
}
所以如果你想检查错误值,你实际上需要调用该Error() string
方法。
此资源解释了基础知识:https ://blog.golang.org/error-handling-and-go
还有 Go 1.13 的基础扩展:https ://blog.golang.org/go1.13-errors
推荐阅读
- c++ - C++ 如何检测 Windows Server 2019?
- python - 无法按名称找到元素
- dialogflow-es - 如何通过 V2 客户端库为节点 js 创建 OutPutContext
- python - 在 Selenium 网页中单击“显示更多交易”
- javascript - 具有 8 个不同值集的对象
- ruby-on-rails - Webpacker:启用监视模式或取消挂钩 webpacker:从资产编译:Rails 中的预编译任务?
- macos - 完全卸载protobuf
- css - Django 应用程序不加载 CSS 背景图像,但图像加载内联样式。有解决办法吗?
- apache-spark - 对于 HIVE 查询,Is OR 提供比 IN 更好的性能
- python - 按行和列在 matplotlib 中迭代生成子图 - 仅绘制最终轴