Skip to content

Commit

Permalink
issue example
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitrii Ovechkin committed Jan 7, 2024
1 parent 5f8c844 commit 21f2cbe
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/check/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ func TestNonInterfaceNotAllowed(t *testing.T) {
_ = Mock[St]()
r.AssertError()
}


60 changes: 60 additions & 0 deletions tests/handler/handler_test.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 21f2cbe

Please sign in to comment.