Skip to content

嵌套测试中,多次mock相同函数,多次运行结果不一致 #90

@zyy329

Description

@zyy329

github.com/bytedance/mockey v1.2.14
go 1.24.4
运行命令: go test -gcflags="all=-l -N" -parallel=1

code:

func funcA() int {
	return 0
}

// 测试多层嵌套关系下 mock 的影响
func TestMockWithConveyS(t *testing.T) {
	mockey.PatchConvey("MockWithConveyS", t, func() {
		convey.So(funcA(), convey.ShouldEqual, 0)

		mockey.PatchConvey("mock 1", func() {
			mockey.Mock(funcA).Return(1).Build()
			convey.So(funcA(), convey.ShouldEqual, 1)

			mockey.PatchConvey("mock 11", func() {
				mockey.Mock(funcA).Return(11).Build()
				convey.So(funcA(), convey.ShouldEqual, 11)
			})
			convey.So(funcA(), convey.ShouldEqual, 1)
		})
	})
}

10次运行中大概有1~2次运行会报错, 比如:
Expected: 1
Actual: 11
(Should equal)!
其余大多数情况能正常通过测试

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions