discordgo_mock

package
v0.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2023 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RoundTripper

type RoundTripper struct {
	mock.Mock
}

RoundTripper is a custom http.RoundTripper set for the target discordgo.Session. It is special in the manner that it is a mock and its http.RoundTrip function acts as a mocked method. This allows in tests to completely intercept the way discordgo handles requests.

func MockSession

func MockSession() (*discordgo.Session, *RoundTripper)

MockSession returns a new discordgo.Session that is modified with custom transport that can be used with testify.

func (*RoundTripper) OnRequestCaptureResult

func (roundTripper *RoundTripper) OnRequestCaptureResult(
	expectedMethod string,
	requestResult interface{},
) *mock.Call

OnRequestCaptureResult tells the mock to expect a request with the given method and fills the requestResult variable with the requests JSON.

requestResult should always be a reference to any empty discordgo struct. The mock.MatchedBy function will take care of trying to decode the received body and fill the requestResult with the data. If filling the body fails, the mock.On will fail.

This might not be the best way of doing things, but its one of the few effective ways that allow relatively easy testing of functionality that uses discordgo.

To modify the expected times a method should be called, use the common mock.Call methods (Once, Times, Twice, ...).

To return a new response, use the common mock.Call Return function. The FillResponse function of this package can be used to populate a dummy response from some struct or slice.

func (*RoundTripper) OnRequestEquals

func (roundTripper *RoundTripper) OnRequestEquals(
	req *http.Request,
) *mock.Call

OnRequestEquals lets the mocked RoundTripper expect a call with the provided response.

To modify the expected times a method should be called, use the common mock.Call methods (Once, Times, Twice, ...).

To return a new response, use the common mock.Call Return function. The FillResponse function of this package can be used to populate a dummy response from some struct or slice.

func (*RoundTripper) RespondWith

func (roundTripper *RoundTripper) RespondWith(call *mock.Call, data interface{}) (*http.Response, error)

RespondWith converts the passed struct to JSON and lets the passed call return a http.Response with the content of the passed struct as data.

func (*RoundTripper) RoundTrip

func (roundTripper *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip handles the request by letting testifies mock implementation handle the processing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL