diff --git a/tests/check/check_test.go b/tests/check/check_test.go index 743e8cd..8e5502e 100644 --- a/tests/check/check_test.go +++ b/tests/check/check_test.go @@ -15,3 +15,5 @@ func TestNonInterfaceNotAllowed(t *testing.T) { _ = Mock[St]() r.AssertError() } + + diff --git a/tests/handler/handler_test.go b/tests/handler/handler_test.go new file mode 100644 index 0000000..821e7de --- /dev/null +++ b/tests/handler/handler_test.go @@ -0,0 +1,60 @@ +package handler + +import ( + . "github.com/ovechkin-dm/mockio/mock" + "github.com/ovechkin-dm/mockio/tests/common" + "testing" +) + +type APIServiceA interface { + GetAll() ([]string, error) + Delete(ID string) error + Add(ID string) error +} + +type APIServiceX interface { + SendMessage(message string) error + IncrementCounterBy(incr int) error +} + +type OtherService struct { + serviceA APIServiceA + serviceX APIServiceX +} + +func (s OtherService) AddAllFromServiceA(strings []string) error { + for _, str := range strings { + err := s.serviceA.Add(str) + if err != nil { + return err + } + } + s.serviceX.IncrementCounterBy(1) + return nil +} + +func TestGetAllFromServiceAUsingMockio(t *testing.T) { + r := common.NewMockReporter(t) + SetUp(r) + mockServiceA := Mock[APIServiceA]() + + When(mockServiceA.Add(AnyString())) + + mockServiceX := Mock[APIServiceX]() + mockServiceX.SendMessage("TEST2") + + s := OtherService{ + serviceA: mockServiceA, + serviceX: mockServiceX, + } + + _ = s.AddAllFromServiceA([]string{"TEST", "OTHER"}) + + _ = Verify(mockServiceA, Once()).Add("TEST") + _ = Verify(mockServiceA, Once()).Add("OTHER") + _ = Verify(mockServiceX, Once()).SendMessage("TEST2") + Verify(mockServiceX, Once()).IncrementCounterBy(1) + VerifyNoMoreInteractions(mockServiceA) + VerifyNoMoreInteractions(mockServiceX) + r.AssertNoError() +}