added error event hooks
This commit is contained in:
@@ -134,6 +134,20 @@ func (scenario *ApiScenario) Test(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// to minimize the breaking changes we always expect the error
|
||||
// events to be called on API error
|
||||
if res.StatusCode >= 400 {
|
||||
if scenario.ExpectedEvents == nil {
|
||||
scenario.ExpectedEvents = map[string]int{}
|
||||
}
|
||||
if _, ok := scenario.ExpectedEvents["OnBeforeApiError"]; !ok {
|
||||
scenario.ExpectedEvents["OnBeforeApiError"] = 1
|
||||
}
|
||||
if _, ok := scenario.ExpectedEvents["OnAfterApiError"]; !ok {
|
||||
scenario.ExpectedEvents["OnAfterApiError"] = 1
|
||||
}
|
||||
}
|
||||
|
||||
if len(testApp.EventCalls) > len(scenario.ExpectedEvents) {
|
||||
t.Errorf("[%s] Expected events %v, got %v", prefix, scenario.ExpectedEvents, testApp.EventCalls)
|
||||
}
|
||||
|
||||
@@ -87,6 +87,16 @@ func NewTestApp(optTestDataDir ...string) (*TestApp, error) {
|
||||
TestMailer: &TestMailer{},
|
||||
}
|
||||
|
||||
t.OnBeforeApiError().Add(func(e *core.ApiErrorEvent) error {
|
||||
t.EventCalls["OnBeforeApiError"]++
|
||||
return nil
|
||||
})
|
||||
|
||||
t.OnAfterApiError().Add(func(e *core.ApiErrorEvent) error {
|
||||
t.EventCalls["OnAfterApiError"]++
|
||||
return nil
|
||||
})
|
||||
|
||||
t.OnModelBeforeCreate().Add(func(e *core.ModelEvent) error {
|
||||
t.EventCalls["OnModelBeforeCreate"]++
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user