@@ -22,8 +22,9 @@ type matchFunc func(interface{}, interface{}) bool
22
22
var stubStorage = stubMapping {}
23
23
24
24
type storage struct {
25
- Input Input
26
- Output Output
25
+ Input Input
26
+ Output Output
27
+ timesCalled int
27
28
}
28
29
29
30
func storeStub (stub * Stub ) error {
@@ -73,24 +74,27 @@ func findStub(stub *findStubPayload) (*Output, error) {
73
74
}
74
75
75
76
closestMatch := []closeMatch {}
76
- for _ , stubrange := range stubs {
77
+ for idx , stubrange := range stubs {
77
78
if expect := stubrange .Input .Equals ; expect != nil {
78
79
closestMatch = append (closestMatch , closeMatch {"equals" , expect })
79
80
if equals (stub .Data , expect ) {
81
+ stubs [idx ].timesCalled ++
80
82
return & stubrange .Output , nil
81
83
}
82
84
}
83
85
84
86
if expect := stubrange .Input .Contains ; expect != nil {
85
87
closestMatch = append (closestMatch , closeMatch {"contains" , expect })
86
88
if contains (stubrange .Input .Contains , stub .Data ) {
89
+ stubs [idx ].timesCalled ++
87
90
return & stubrange .Output , nil
88
91
}
89
92
}
90
93
91
94
if expect := stubrange .Input .Matches ; expect != nil {
92
95
closestMatch = append (closestMatch , closeMatch {"matches" , expect })
93
96
if matches (stubrange .Input .Matches , stub .Data ) {
97
+ stubs [idx ].timesCalled ++
94
98
return & stubrange .Output , nil
95
99
}
96
100
}
@@ -318,3 +322,33 @@ func (sm *stubMapping) readStubFromFile(path string) {
318
322
sm .storeStub (stub )
319
323
}
320
324
}
325
+
326
+ func getStubTimesCalled (stub * verifyStubCallPayload ) (* Output , error ) {
327
+ mx .Lock ()
328
+ defer mx .Unlock ()
329
+
330
+ if _ , ok := stubStorage [stub .Service ]; ! ok {
331
+ return nil , fmt .Errorf ("Can't find stub for Service: %s" , stub .Service )
332
+ }
333
+
334
+ if _ , ok := stubStorage [stub.Service ][stub.Method ]; ! ok {
335
+ return nil , fmt .Errorf ("Can't find stub for Service:%s and Method:%s" , stub .Service , stub .Method )
336
+ }
337
+
338
+ stubs := stubStorage [stub.Service ][stub.Method ]
339
+ if len (stubs ) == 0 {
340
+ return nil , fmt .Errorf ("Stub for Service:%s and Method:%s is empty" , stub .Service , stub .Method )
341
+ }
342
+
343
+ totalTimesCalled := 0
344
+
345
+ for _ , stubrange := range stubs {
346
+ totalTimesCalled += stubrange .timesCalled
347
+ }
348
+
349
+ return & Output {
350
+ Data : map [string ]interface {}{
351
+ "timesCalled" : totalTimesCalled ,
352
+ },
353
+ }, nil
354
+ }
0 commit comments