本文整理汇总了Golang中github.com/stretchr/testify/mock.AssertExpectationsForObjects函数的典型用法代码示例。如果您正苦于以下问题:Golang AssertExpectationsForObjects函数的具体用法?Golang AssertExpectationsForObjects怎么用?Golang AssertExpectationsForObjects使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了AssertExpectationsForObjects函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestMarshalWithCodec_WithFacade
func TestMarshalWithCodec_WithFacade(t *testing.T) {
// func (s *WebCodecService) MarshalWithCodec(codec codecs.Codec, object interface{}, options ...interface{}) ([]byte, error) {
testCodec := new(test.TestCodec)
service := NewWebCodecService()
// make some test stuff
var bytesToReturn []byte = []byte("Hello World")
testObjectWithFacade := new(test.TestObjectWithFacade)
object := objx.MSI("Name", "Mat")
var option1 string = "Option One"
var option2 string = "Option Two"
args := map[string]interface{}{option1: option1, option2: option2}
// setup expectations
testObjectWithFacade.On("PublicData", args).Return(object, nil)
testCodec.On("Marshal", object, args).Return(bytesToReturn, nil)
bytes, err := service.MarshalWithCodec(testCodec, testObjectWithFacade, args)
if assert.Nil(t, err) {
assert.Equal(t, string(bytesToReturn), string(bytes))
}
// assert that our expectations were met
mock.AssertExpectationsForObjects(t, testCodec.Mock, testObjectWithFacade.Mock)
}
示例2: TestPublicData_WithArray
func TestPublicData_WithArray(t *testing.T) {
o := new(test.TestObjectWithFacade)
o1 := new(test.TestObjectWithFacade)
o2 := new(test.TestObjectWithFacade)
arr := []interface{}{o, o1, o2}
o.Mock.On("PublicData", map[string]interface{}{}).Return(objx.New(map[string]interface{}{"theName": "1"}), nil)
o1.Mock.On("PublicData", map[string]interface{}{}).Return(objx.New(map[string]interface{}{"theName": "2"}), nil)
o2.Mock.On("PublicData", map[string]interface{}{}).Return(objx.New(map[string]interface{}{"theName": "3"}), nil)
public, err := PublicData(arr, map[string]interface{}{})
if assert.Nil(t, err) {
assert.Equal(t, reflect.Slice, reflect.TypeOf(public).Kind(), "Result should be array not %v", reflect.TypeOf(public))
}
mock.AssertExpectationsForObjects(t, o.Mock, o1.Mock, o2.Mock)
publicArray := public.([]interface{})
if assert.Equal(t, 3, len(publicArray)) {
assert.Equal(t, publicArray[0].(objx.Map).Get("theName").Str(), "1", "o")
assert.Equal(t, publicArray[1].(objx.Map).Get("theName").Str(), "2", "o1")
assert.Equal(t, publicArray[2].(objx.Map).Get("theName").Str(), "3", "o2")
}
}
示例3: TestErrorHandlerGetsUsedOnError
func TestErrorHandlerGetsUsedOnError(t *testing.T) {
responseWriter := new(http_test.TestResponseWriter)
testRequest, _ := http.NewRequest("GET", "http://stretchr.org/goweb", nil)
codecService := codecsservices.NewWebCodecService()
handler := NewHttpHandler(codecService)
errorHandler := new(handlers_test.TestHandler)
handler.SetErrorHandler(errorHandler)
errorHandler.On("Handle", mock.Anything).Return(false, nil)
// make a handler throw an error
var theError error = errors.New("Test error")
handler.Map(func(c context.Context) error {
return theError
})
handler.ServeHTTP(responseWriter, testRequest)
if mock.AssertExpectationsForObjects(t, errorHandler.Mock) {
// get the first context
ctx := errorHandler.Calls[0].Arguments[0].(context.Context)
// make sure the error data field was set
assert.Equal(t, theError.Error(), ctx.Data().Get("error").Data().(HandlerError).Error(), "the error should be set in the data with the 'error' key")
assert.Equal(t, responseWriter, ctx.HttpResponseWriter())
assert.Equal(t, testRequest, ctx.HttpRequest())
}
}
示例4: TestMarshalWithCodec_WithError
func TestMarshalWithCodec_WithError(t *testing.T) {
// func (s *WebCodecService) MarshalWithCodec(codec codecs.Codec, object interface{}, options ...interface{}) ([]byte, error) {
testCodec := new(test.TestCodec)
service := NewWebCodecService()
// make some test stuff
object := objx.MSI("Name", "Mat")
var option1 string = "Option One"
var option2 string = "Option Two"
args := map[string]interface{}{option1: option1, option2: option2}
// setup expectations
testCodec.On("Marshal", object, args).Return(nil, assert.AnError)
_, err := service.MarshalWithCodec(testCodec, object, args)
assert.Equal(t, assert.AnError, err, "The error should get returned")
// assert that our expectations were met
mock.AssertExpectationsForObjects(t, testCodec.Mock)
}
示例5: TestPipe_Handle
func TestPipe_Handle(t *testing.T) {
ctx := context_test.MakeTestContext()
handler1 := new(handlers_test.TestHandler)
handler2 := new(handlers_test.TestHandler)
handler3 := new(handlers_test.TestHandler)
// add the handlers to the pipe
p := Pipe{handler1, handler2, handler3}
// setup expectations
handler1.On("WillHandle", ctx).Return(true, nil)
handler1.On("Handle", ctx).Return(false, nil)
handler2.On("WillHandle", ctx).Return(false, nil)
handler3.On("WillHandle", ctx).Return(true, nil)
handler3.On("Handle", ctx).Return(false, nil)
// call handle
p.Handle(ctx)
// assert expectations
mock.AssertExpectationsForObjects(t, handler1.Mock, handler2.Mock, handler3.Mock)
}
示例6: TestMarshalWithCodec
func TestMarshalWithCodec(t *testing.T) {
testCodec := new(test.TestCodec)
service := NewWebCodecService()
// make some test stuff
var bytesToReturn []byte = []byte("Hello World")
var object objx.Map = objx.MSI("Name", "Mat")
var option1 string = "Option One"
var option2 string = "Option Two"
args := map[string]interface{}{option1: option1, option2: option2}
// setup expectations
testCodec.On("Marshal", object, args).Return(bytesToReturn, nil)
bytes, err := service.MarshalWithCodec(testCodec, object, args)
if assert.Nil(t, err) {
assert.Equal(t, string(bytesToReturn), string(bytes))
}
// assert that our expectations were met
mock.AssertExpectationsForObjects(t, testCodec.Mock)
}
示例7: TestOAuth2Provider_Non200Response
func TestOAuth2Provider_Non200Response(t *testing.T) {
config := &common.Config{
Map: objx.MSI(
OAuth2KeyRedirectUrl, OAuth2KeyRedirectUrl,
OAuth2KeyScope, OAuth2KeyScope,
OAuth2KeyClientID, OAuth2KeyClientID,
OAuth2KeySecret, OAuth2KeySecret,
OAuth2KeyAuthURL, OAuth2KeyAuthURL,
OAuth2KeyTokenURL, OAuth2KeyTokenURL)}
testTripperFactory := new(test.TestTripperFactory)
testTripper := new(test.TestTripper)
testProvider := new(test.TestProvider)
testResponse := new(http.Response)
testResponse.Header = make(http.Header)
testResponse.Header.Set("Content-Type", "text/plain")
testResponse.StatusCode = 401
testResponse.Body = ioutil.NopCloser(strings.NewReader("No mate"))
testTripperFactory.On("NewTripper", common.EmptyCredentials, mock.Anything).Return(testTripper, nil)
testTripper.On("RoundTrip", mock.Anything).Return(testResponse, nil)
data := objx.MSI(OAuth2KeyCode, []string{"123"})
_, err := CompleteAuth(testTripperFactory, data, config, testProvider)
if assert.Error(t, err) {
assert.IsType(t, &common.AuthServerError{}, err)
}
mock.AssertExpectationsForObjects(t, testTripperFactory.Mock, testTripper.Mock, testProvider.Mock)
}
示例8: TestNewUser
func TestNewUser(t *testing.T) {
testProvider := new(test.TestProvider)
testProvider.On("Name").Return("providerName")
data := objx.MSI(
spotifyKeyID, "123435467890",
spotifyKeyName, "Mathew",
spotifyKeyEmail, "[email protected]",
spotifyKeyNickname, "Mat",
)
creds := &common.Credentials{Map: objx.MSI(oauth2.OAuth2KeyAccessToken, "ABC123")}
user := NewUser(data, creds, testProvider)
if assert.NotNil(t, user) {
assert.Equal(t, data, user.Data())
assert.Equal(t, "Mathew", user.Name())
assert.Equal(t, "[email protected]", user.Email())
assert.Equal(t, "Mat", user.Nickname())
// check provider credentials
creds := user.ProviderCredentials()[testProvider.Name()]
if assert.NotNil(t, creds) {
assert.Equal(t, "ABC123", creds.Get(oauth2.OAuth2KeyAccessToken).Str())
assert.Equal(t, "123435467890", creds.Get(common.CredentialsKeyID).Str())
}
}
mock.AssertExpectationsForObjects(t, testProvider.Mock)
}
示例9: TestProvider
func TestProvider(t *testing.T) {
prov1 := new(test.TestProvider)
prov2 := new(test.TestProvider)
prov1.On("Name").Return("prov1")
prov2.On("Name").Return("prov2")
// build a list
list := WithProviders(prov1, prov2)
returnedProv, err := list.Provider("prov1")
if assert.NoError(t, err) {
assert.Equal(t, returnedProv, prov1)
}
// check nonsense name
returnedProv, err = Provider("no such provider")
if assert.Nil(t, returnedProv) {
assert.IsType(t, &common.MissingProviderError{}, err, "MissingProviderError expected")
}
mock.AssertExpectationsForObjects(t, prov1.Mock, prov2.Mock)
}
示例10: TestNewUser
func TestNewUser(t *testing.T) {
testProvider := new(test.TestProvider)
testProvider.On("Name").Return("providerName")
data := objx.MSI(
instagramKeyID, "123435467890",
instagramKeyName, "Raquel",
instagramKeyNickname, "maggit",
instagramKeyAvatarUrl, "http://instagram.com/")
creds := &common.Credentials{Map: objx.MSI(oauth2.OAuth2KeyAccessToken, "ABC12345")}
user := NewUser(data, creds, testProvider)
if assert.NotNil(t, user) {
assert.Equal(t, data, user.Data())
assert.Equal(t, "Raquel", user.Name())
assert.Equal(t, "maggit", user.Nickname())
assert.Equal(t, "http://instagram.com/", user.AvatarURL())
// check provider credentials
creds := user.ProviderCredentials()[testProvider.Name()]
if assert.NotNil(t, creds) {
assert.Equal(t, "ABC12345", creds.Get(oauth2.OAuth2KeyAccessToken).Str())
assert.Equal(t, "123435467890", creds.Get(common.CredentialsKeyID).Str())
}
}
mock.AssertExpectationsForObjects(t, testProvider.Mock)
}
示例11: TestPrependPreHandler
func TestPrependPreHandler(t *testing.T) {
handler1 := new(handlers_test.TestHandler)
handler2 := new(handlers_test.TestHandler)
codecService := codecsservices.NewWebCodecService()
h := NewHttpHandler(codecService)
handler1.TestData().Set("id", 1)
handler2.TestData().Set("id", 2)
handler1.On("WillHandle", mock.Anything).Return(true, nil)
handler1.On("Handle", mock.Anything).Return(false, nil)
handler2.On("WillHandle", mock.Anything).Return(true, nil)
handler2.On("Handle", mock.Anything).Return(false, nil)
h.PrependPreHandler(handler1)
h.PrependPreHandler(handler2)
h.Handlers.Handle(nil)
assert.Equal(t, 2, len(h.PreHandlersPipe()))
assert.Equal(t, 2, h.PreHandlersPipe()[0].(*handlers_test.TestHandler).TestData().Get("id").Data())
assert.Equal(t, 1, h.PreHandlersPipe()[1].(*handlers_test.TestHandler).TestData().Get("id").Data())
mock.AssertExpectationsForObjects(t, handler1.Mock)
}
示例12: TestRoundTrip
func TestRoundTrip(t *testing.T) {
underlyingTripper := new(testifyhttp.TestRoundTripper)
testProvider := new(test.TestProvider)
creds := &common.Credentials{Map: objx.MSI()}
creds.Set(OAuth2KeyAccessToken, "This is a real access token :)")
tripper := new(OAuth2Tripper)
tripper.underlyingTransport = underlyingTripper
tripper.credentials = creds
tripper.provider = testProvider
request, _ := http.NewRequest("GET", "something", nil)
underlyingTripper.On("RoundTrip", mock.Anything).Return(new(http.Response), nil)
response, err := tripper.RoundTrip(request)
if assert.NoError(t, err) {
if assert.NotNil(t, response) {
actualRequest := underlyingTripper.Calls[0].Arguments[0].(*http.Request)
if assert.NotEqual(t, &actualRequest, &request, "Actual request should be different") {
headerK, headerV := AuthorizationHeader(creds)
assert.Equal(t, actualRequest.Header.Get(headerK), headerV)
}
}
}
mock.AssertExpectationsForObjects(t, testProvider.Mock, underlyingTripper.Mock)
}
示例13: TestPublicData_WithError
func TestPublicData_WithError(t *testing.T) {
o := new(test.TestObjectWithFacade)
o.Mock.On("PublicData", map[string]interface{}{}).Return(nil, assert.AnError)
_, err := PublicData(o, map[string]interface{}{})
assert.Equal(t, assert.AnError, err)
mock.AssertExpectationsForObjects(t, o.Mock)
}
示例14: TestPublicDataMap_ReturningNil
func TestPublicDataMap_ReturningNil(t *testing.T) {
o := new(test.TestObjectWithFacade)
o.Mock.On("PublicData", map[string]interface{}{}).Return(nil, nil)
public, err := PublicDataMap(o, map[string]interface{}{})
if assert.Nil(t, err) {
assert.Nil(t, public)
}
mock.AssertExpectationsForObjects(t, o.Mock)
}
示例15: TestPublicData
func TestPublicData(t *testing.T) {
o := new(test.TestObjectWithFacade)
o.Mock.On("PublicData", map[string]interface{}{}).Return(objects.Map{"theName": "Mat"}, nil)
public, err := PublicData(o, map[string]interface{}{})
if assert.Nil(t, err) {
assert.Equal(t, public.(objects.Map)["theName"], "Mat")
}
mock.AssertExpectationsForObjects(t, o.Mock)
}