Ginkgo: 为单元测试模拟一个方法

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

Ginkgo: Mocking a method for unit test

问题

请注意,结构体 S 实现了接口 I
我正在尝试通过模拟来测试 MethodA 的 MethodB 响应。

sample.go:

  1. package service
  2. // 这是由 S 实现的接口
  3. type I interface {
  4. MethodA(num int) int
  5. MethodB(num int) int
  6. }
  7. type S struct {
  8. num int
  9. str string
  10. }
  11. func NewI(num int, str string) I {
  12. return S{num, str}
  13. }
  14. func (s S) MethodA(num int) int {
  15. resp := s.MethodB(num) // 想要模拟这个方法
  16. return 5 * resp
  17. }
  18. func (s S) MethodB(num int) int {
  19. return num * 10
  20. }

sample_test.go :

  1. package service
  2. import (
  3. . "github.com/onsi/ginkgo"
  4. . "github.com/onsi/gomega"
  5. )
  6. type MockI struct {
  7. MockMethodA func(num int) int
  8. MockMethodB func(num int) int
  9. }
  10. func (m *MockI) MethodA(num int) int {
  11. return m.MockMethodA(num)
  12. }
  13. func (m *MockI) MethodB(num int) int {
  14. return m.MockMethodB(num)
  15. }
  16. var _ = Describe("MethodA", func() {
  17. Context("MethodA", func() {
  18. Describe("normal case", func() {
  19. It("should give proper response", func() {
  20. i := NewI(1, "test")
  21. // 必须模拟 methodB()
  22. // 类似于这样:
  23. // i.MethodB = MethodB(num int) int{
  24. // return <some_value>
  25. // }
  26. got := i.MethodA(10)
  27. expected := 500
  28. Expect(got).To(Equal(expected))
  29. })
  30. })
  31. })
  32. })

任何帮助将不胜感激。谢谢!

英文:

Please note that struct S implements the interface I.
I'm trying to test MethodA by mocking the response from MethodB.

sample.go:

  1. package service
  2. // This is implemented by S
  3. type I interface {
  4. MethodA(num int) int
  5. MethodB(num int) int
  6. }
  7. type S struct {
  8. num int
  9. str string
  10. }
  11. func NewI(num int, str string) I {
  12. return S{num, str}
  13. }
  14. func (s S) MethodA(num int) int {
  15. resp := s.MethodB(num) // want to mock this
  16. return 5 * resp
  17. }
  18. func (s S) MethodB(num int) int {
  19. return num * 10
  20. }

sample_test.go :

  1. package service
  2. import (
  3. . &quot;github.com/onsi/ginkgo&quot;
  4. . &quot;github.com/onsi/gomega&quot;
  5. )
  6. type MockI struct {
  7. MockMethodA func(num int) int
  8. MockMethodB func(num int) int
  9. }
  10. func (m *MockI) MethodA(num int) int {
  11. return m.MockMethodA(num)
  12. }
  13. func (m *MockI) MethodB(num int) int {
  14. return m.MockMethodB(num)
  15. }
  16. var _ = Describe(&quot;MethodA&quot;, func() {
  17. Context(&quot;MethodA&quot;, func() {
  18. Describe(&quot;normal case&quot;, func() {
  19. It(&quot;should give proper response&quot;, func() {
  20. i := NewI(1, &quot;test&quot;)
  21. // have to mock methodB()
  22. // something like this:
  23. // i.MethodB = MethodB(num int) int{
  24. // return &lt;some_value&gt;
  25. // }
  26. got := i.MethodA(10)
  27. expected := 500
  28. Expect(got).To(Equal(expected))
  29. })
  30. })
  31. })
  32. })

Any help will be appreciated. Thanks!

答案1

得分: 1

使用依赖注入不行吗?将MockI注入到要测试的代码中。

  1. func funcTobeTested(i I) {
  2. i.MethodA(0)
  3. }

然后在测试中:

  1. mockI := MockI{}
  2. // 将运行MethodA的模拟实现
  3. funcTobeTested(mockI)
英文:

Wouldnt the usage of dependency injection work? Inject the MockI into the code that you will be testing.

  1. func funcTobeTested(i I) {
  2. i.MethodA(0)
  3. }

And then on the test:

  1. mockI := MockI{}
  2. // Will run the mock implementation of MethodA
  3. funcTobeTested(mockI)

答案2

得分: 0

我认为你可以使用mockery包来模拟接口,并使用mock包的.On函数来在测试methodA时模拟methodB。

英文:

I think you can use the mockery package to mock the interface and use the .On function of mock package to mock just methodB in testing methodA.

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

发表评论

匿名网友

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

确定