Beispiel #1
0
func TestFluentStubbingWithError(t *testing.T) {
	mock := NewMock()
	mock.
		On(mockFooService, mockHealthEndpoint).
		Fail(errors.BadRequest("code", "description"))

	req, _ := client.NewRequest(mockFooService, mockHealthEndpoint, NewDummy("ping"))
	rsp := &Dummy{}

	// Fail with given error
	err := mock.Caller()(req, rsp)
	assert.NotNil(t, err)
	assert.Equal(t, err.Code(), "code")
	assert.Equal(t, err.Description(), "description")
}
Beispiel #2
0
func (suite *multiClientSuite) TestAnyErrorsIgnoring() {
	cases := []struct {
		errs         map[string]errors.Error
		types, codes []string
		isErr        bool
	}{
		{
			errs:  map[string]errors.Error{},
			types: []string{},
			codes: []string{},
			isErr: false,
		},
		// allow nils
		{
			errs:  map[string]errors.Error{},
			types: nil,
			codes: nil,
			isErr: false,
		},
		// no errors = always false
		{
			errs:  map[string]errors.Error{},
			types: []string{errors.ErrorForbidden, errors.ErrorBadResponse},
			codes: []string{"com.hailocab.service.foo.bar"},
			isErr: false,
		},
		{
			errs:  map[string]errors.Error{},
			types: []string{errors.ErrorForbidden, errors.ErrorBadResponse},
			codes: []string{},
			isErr: false,
		},
		{
			errs:  map[string]errors.Error{},
			types: []string{},
			codes: []string{"com.hailocab.service.foo.bar"},
			isErr: false,
		},
		{
			errs:  map[string]errors.Error{},
			types: []string{errors.ErrorForbidden, errors.ErrorBadResponse},
			codes: nil,
			isErr: false,
		},
		{
			errs:  map[string]errors.Error{},
			types: nil,
			codes: []string{"com.hailocab.service.foo.bar"},
			isErr: false,
		},
		// types
		{
			errs: map[string]errors.Error{
				"foo": errors.BadResponse("foo.bar", "ZOMG"),
			},
			types: []string{errors.ErrorForbidden, errors.ErrorBadResponse},
			codes: []string{},
			isErr: false,
		},
		{
			errs: map[string]errors.Error{
				"foo": errors.BadRequest("foo.bar", "ZOMG"),
			},
			types: []string{errors.ErrorForbidden, errors.ErrorBadResponse},
			codes: []string{},
			isErr: true,
		},
		{
			errs: map[string]errors.Error{
				"foo": errors.BadResponse("foo.bar", "ZOMG"),
				"bar": errors.BadRequest("foo.bar", "ZOMG"),
			},
			types: []string{errors.ErrorForbidden, errors.ErrorBadResponse},
			codes: []string{},
			isErr: true,
		},
		// codes
		{
			errs: map[string]errors.Error{
				"foo": errors.BadResponse("foo.bar", "ZOMG"),
			},
			types: nil,
			codes: []string{"foo.bar"},
			isErr: false,
		},
		{
			errs: map[string]errors.Error{
				"foo": errors.BadResponse("foo.bar", "ZOMG"),
			},
			types: nil,
			codes: []string{"foo.bar.baz"},
			isErr: true,
		},
		{
			errs: map[string]errors.Error{
				"foo": errors.BadResponse("foo.bar", "ZOMG"),
				"bar": errors.BadResponse("foo.bar.baz", "ZOMG"),
			},
			types: nil,
			codes: []string{"foo.bar"},
			isErr: true,
		},
	}

	cl := &defClient{
		errors: &errorsImpl{},
	}

	for _, tc := range cases {
		for uid, err := range tc.errs {
			cl.errors.set(uid, &client.Request{}, err, nil)
		}

		res := cl.AnyErrorsIgnoring(tc.types, tc.codes)
		suite.Assertions.Equal(tc.isErr, res, fmt.Sprintf("Wrong result for errors: %v - types: %v - codes: %v",
			tc.errs, tc.types, tc.codes))
	}
}