donglu1472 2017-01-18 12:46
浏览 24

如何在Golang中的相同结构方法中模拟结构方法?

I have troubles with mocking.. I have same code:

type OtherInterface interface {
   Do() error
}
type MyInterface interface {
   MethodA() error
   MethodB() error
}
type MyStruct struct {
  OtherStruct OtherInterface
}
func(s *MyStruct) MethodA() error {
  return s.MethodB()
}
func(s *MyStruct) MethodB() error {
  return s.OtherStruct.Do()
}

And test code of MethodB:

type MockOtherStruct struct {
  Do_Error error
}
func (t *MockOtherStruct) Do() error {return t.Do_Error}


TestMyStruct_MethodB(t *testing.B) {
  expectedError = nil
  mos := &MockOtherStruct{
    Do_Error: nil
  }
  ms := MyStruct{
   OtherStruct: mos,
  }
  err := ms.MethodB()
  if err != expectedError {
    t.Fatal(err)
  }      
}

But how can I test MethodA?

Yes, I can also mock OtherStruct and use not mocked MethodB, but in big structs I can have many fields with other structs, but use one internal method..

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统