Weitere ähnliche Inhalte Ähnlich wie Mock-n-stub (11) Kürzlich hochgeladen (20) Mock-n-stub2. Пирожок Без НиктоПирожок Без Никто
• Создавать дополнительные объектыСоздавать дополнительные объекты
• " "Создавать временную реализацию протокола" "Создавать временную реализацию протокола
• Оставлять затычкиОставлять затычки #ifdef DEBUG ...#ifdef DEBUG ...
• Всякие радости с бэкэндомВсякие радости с бэкэндом
4. Вот эти ребятаВот эти ребята
• OCMockOCMock
• OCMockitoOCMockito
• NocillaNocilla
• OHHTTPStubsOHHTTPStubs
• Sandboxи немного Sandboxи немного
5. Два брата-акробатаДва брата-акробата
• mock-Создавать объекты для классовmock-Создавать объекты для классов
OCMClassMockOCMClassMock((aClassaClass))
MKTMockMKTMock((aClassaClass))
• mock-Создавать объекты для протоколовmock-Создавать объекты для протоколов
OCMProtocolMockOCMProtocolMock((aProtocolaProtocol))
MKTMockProtocolMKTMockProtocol((aProtocolaProtocol))
• OCMockitoУ есть дополнительные методыOCMockitoУ есть дополнительные методы
MKTMockObjectAndProtocolMKTMockObjectAndProtocol((aClassaClass,, aProtocolaProtocol))
MKTMockProtocolWithoutOptionalsMKTMockProtocolWithoutOptionals((aProtocolaProtocol))
• OCMockУ есть дополнительные методыOCMockУ есть дополнительные методы
OCMStrictClassMockOCMStrictClassMock((aClassaClass))
OCMStrictProtocolMockOCMStrictProtocolMock((aProtocolaProtocol))
OCMPartialMockOCMPartialMock((aObjectaObject))
OCMObserverMockOCMObserverMock()()
6. Stubbing methodsStubbing methods
idid<ContentProviderProtocol> contentProvider = ...<ContentProviderProtocol> contentProvider = ...
OCMStubOCMStub([contentProvider([contentProvider loadSomeValuesloadSomeValues]).]).andReturnandReturn((@[@1, @3, @2]@[@1, @3, @2]););
[[MKTGivenMKTGiven([contentProvider([contentProvider loadSomeValuesloadSomeValues])]) willReturnwillReturn::@[@1, @3, @2]@[@1, @3, @2]];];
OCMockitoOCMockito
--willReturnwillReturn%primitiveTypeName%:aValue%primitiveTypeName%:aValue
--willReturnStructwillReturnStruct:aStructPointer:aStructPointer objCTypeobjCType:aType:aType
--willDowillDo:aBlock:aBlock
--willThrowwillThrow:anException:anException
OCMockOCMock
..andReturnandReturn(aValue)(aValue)
..andThrowandThrow(anException)(anException)
..andPostandPost(aNotification)(aNotification)
..andCallandCall(anObject, aSelector)(anObject, aSelector)
..andDoandDo(aBlock)(aBlock)
..andForwardToRealObjectandForwardToRealObject()()
7. Chaining stub actionsChaining stub actions
OCMStubOCMStub([contentProvider([contentProvider loadSomeValuesloadSomeValues]).]).andReturnandReturn((@[@1, @3,@[@1, @3,
@2]@2]).).andThrowandThrow((willThrowwillThrow:anException);:anException);
[[[[MKTGivenMKTGiven([contentProvider([contentProvider loadSomeValuesloadSomeValues])]) willReturnwillReturn::@[@1, @3, @2]@[@1, @3, @2]]]
willThrowwillThrow:anException];:anException];
8. Advanced stubbing methodsAdvanced stubbing methods
NSMutableArrayNSMutableArray *mockArray = ...*mockArray = ...
OCMStubOCMStub([mockArray([mockArray addObject:@1addObject:@1]);]);
OCMArgOCMArg *arg =*arg = [[OCMArgOCMArg isKindOfClassisKindOfClass:[:[NSStringNSString classclass]];]];
OCMStubOCMStub([mockArray([mockArray addObjectaddObject:arg]).:arg]).andThrowandThrow(anException);(anException);
OCMArgOCMArg
--isNilisNil
--isNotNilisNotNil
--isNotEqualisNotEqual:aValue:aValue
--isKindOfClassisKindOfClass:aClass:aClass
--checkWithSelectorcheckWithSelector:aSelector:aSelector onObjectonObject:anObject:anObject
--checkWithBlockcheckWithBlock:^:^BOOLBOOL((idid value)blockvalue)block
[[MKTGivenMKTGiven([mockArray([mockArray containsObjectcontainsObject::startsWithstartsWith((@"tes"@"tes")]))]) willReturnBoolwillReturnBool::YESYES];];
9. Verifying interactionsVerifying interactions
NSMutableArrayNSMutableArray *mockArray = ...*mockArray = ...
[mockArray[mockArray removeObjectremoveObject::@"This is a test"@"This is a test"];];
[[MKTVerifyMKTVerify(mockArray)(mockArray) removeObjectremoveObject::startsWithstartsWith((@"This is"@"This is")];)];
OCMVerifyOCMVerify([mockArray([mockArray removeObjectremoveObject::startsWithstartsWith((@"This is"@"This is")]);)]);
OCMExpectOCMExpect([mockArray([mockArray removeObjectremoveObject::startsWithstartsWith((@"This is"@"This is")]);)]);
OCMExpectOCMExpect([mockArray([mockArray removeAllObjectsremoveAllObjects]);]);
......
OCMVerifyAllOCMVerifyAll(classMock)(classMock)
10. NocillaNocilla
+ (+ (voidvoid)setUp)setUp
{{
[[[[LSNocillaLSNocilla sharedInstancesharedInstance]] startstart];];
}}
- (- (voidvoid)setUp)setUp
{{
[[[[LSNocillaLSNocilla sharedInstancesharedInstance]] clearStubsclearStubs];];
}}
+ (+ (voidvoid)tearDown)tearDown
{{
[[[[LSNocillaLSNocilla sharedInstancesharedInstance]] stopstop];];
}}
11. Stubbing requestsStubbing requests
stubRequeststubRequest((@"GET"@"GET",, @"http://mockingbird.getsandbox.com/selectedColor"@"http://mockingbird.getsandbox.com/selectedColor"))
..withHeaderswithHeaders((@{@{@"Accept"@"Accept":: @"application/json"@"application/json"}}))
..andReturnandReturn((200200))
..withHeaderswithHeaders((@{@{@"Content-Type"@"Content-Type":: @"application/json"@"application/json"}}))
..withBodywithBody((@"{"selectedColor":[1, 0, 0, 1]}"@"{"selectedColor":[1, 0, 0, 1]}"););
andReturnRawResponseandReturnRawResponse(aData);(aData);
andFailWithErrorandFailWithError(anError);(anError);
12. OHHTTPStubsOHHTTPStubs
[[OHHTTPStubsOHHTTPStubs stubRequestsPassingTeststubRequestsPassingTest:^:^BOOLBOOL((NSURLRequestNSURLRequest *request) {*request) {
......
returnreturn isRequestAcceptedByStub;isRequestAcceptedByStub;
}} withStubResponsewithStubResponse:^:^OHHTTPStubsResponseOHHTTPStubsResponse*(*(NSURLRequestNSURLRequest *request) {*request) {
returnreturn httpStubsResponse;httpStubsResponse;
}];}];
OHHTTPStubsResponseOHHTTPStubsResponse
++ responseWithDataresponseWithData::statusCode:headersstatusCode:headers::
++ responseWithFileAtPathresponseWithFileAtPath::statusCode:headersstatusCode:headers::
++ responseWithErrorresponseWithError::
++ responseWithJSONObjectresponseWithJSONObject::statusCodestatusCode::headersheaders::
++ responseWithHTTPMessageDataresponseWithHTTPMessageData::
++ responseNamed:inBundleresponseNamed:inBundle::
15. Всем спасибо за внимание!Всем спасибо за внимание!
• OCMock -OCMock - http://ocmock.org/http://ocmock.org/
• OCMockito -OCMockito - https://github.com/jonreid/OCMockitohttps://github.com/jonreid/OCMockito
• Nocilla -Nocilla - https://github.com/luisobo/Nocillahttps://github.com/luisobo/Nocilla
• OHHTTPStubs -OHHTTPStubs -
https://github.com/AliSoftware/OHHTTPStubshttps://github.com/AliSoftware/OHHTTPStubs
• Sandbox -Sandbox - https://getsandbox.com/https://getsandbox.com/