-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dmitrii Ovechkin
committed
Jan 7, 2024
1 parent
5f8c844
commit 21f2cbe
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,5 @@ func TestNonInterfaceNotAllowed(t *testing.T) { | |
_ = Mock[St]() | ||
r.AssertError() | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |