如何在我的测试中检查方法是否返回具有指定持续时间的 time.ticker?

huangapple go评论83阅读模式
英文:

How can I check in my test that the method returns a time.ticker with the specified duration?

问题

如何测试此方法的正确返回值?

func CreateTicker(dur time.Duration) *time.Ticker {
	return time.NewTicker(dur * time.Second)
}
func TestCreateTicker(t *testing.T) {
	type args struct {
		dur time.Duration
	}
	var (
		ticker = time.NewTicker(12)
	)

	tests := []struct {
		name string
		args args
		want *time.Ticker
	}{
		{
			name: "test",
			args: args{dur: 12},
			want: ticker,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := CreateTicker(tt.args.dur); !reflect.DeepEqual(got, tt.want) {
				t.Errorf("CreateTicker() = %v, want %v", got, tt.want)
			}
		})
	}
}

当我运行测试时,我得到了对象不相同的错误消息。

--- FAIL: TestCreateTicker (0.00s)
    --- FAIL: TestCreateTicker/test. (0.00s)
      CreateTicker() = &{0xc000062360 {824633909248 32883400129906 12000000000 0x471bc0 0xc000062360 0 0 1}}, want &{0xc0000622a0 {824633909248 32871400127913 12 0x471bc0 0xc0000622a0 0 0 1}}
FAIL
...
英文:

How can I test for the correct return value in this method?

func CreateTicker(dur time.Duration) *time.Ticker {
	return time.NewTicker(dur * time.Second)
}
func TestCreateTicker(t *testing.T) {
	type args struct {
		dur time.Duration
	}
	var (
		ticker = time.NewTicker(12)
	)

	tests := []struct {
		name string
		args args
		want *time.Ticker
	}{
		{
			name: "test",
			args: args{dur: 12},
			want: ticker,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := CreateTicker(tt.args.dur); !reflect.DeepEqual(got, tt.want) {
				t.Errorf("CreateTicker() = %v, want %v", got, tt.want)
			}
		})
	}
}

At the moment when I run the test I get the error message that the objects are not identical.

--- FAIL: TestCreateTicker (0.00s)
    --- FAIL: TestCreateTicker/test. (0.00s)
      CreateTicker() = &{0xc000062360 {824633909248 32883400129906 12000000000 0x471bc0 0xc000062360 0 0 1}}, want &{0xc0000622a0 {824633909248 32871400127913 12 0x471bc0 0xc0000622a0 0 0 1}}
FAIL
...

答案1

得分: 2

如果你真的想测试这个,你需要实际测量计时器发出两个滴答声所需的时间,并检查差值是否在可接受范围内。这需要很大的努力,而这个测试并没有太大的用处,而且你的测试代码比例已经显示出很差的回报率,25行的测试代码覆盖了1行的函数。

更好的解决方案是不要测试这个。这只是一个调用标准库函数的单行代码,你可以假设它已经经过了充分的测试。

英文:

If you really want to test this, you would have to actually measure how long it takes the timer to emit two ticks, and check whether the delta is within an acceptable range. That's a lot of effort for a test that is not very useful, and your test-to-code ratio is already showing quite a poor return on investment, with 25 lines of test covering a 1 line function.

The better solution is to not test this. It's a single line that calls out to a standard library function, which you can assume is well tested.

huangapple
  • 本文由 发表于 2022年6月3日 23:37:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/72491989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定