As with all other code, your Combine pipeline code can, and should be, tested. The Given-When-Then pattern is a great way to
layout your test code, ensuring that your code works as intended.
This content was released on Apr 13 2021. The official support period is 6-months
from this date.
As with all other code, your Combine pipeline code can, and should be, tested. The Given-When-Then pattern is a great way to
layout your test code, ensuring that your code works as intended.
Cinema mode
Mark complete
Download course materials
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
Previous episode: 05. Retrying and Catching Errors
Next episode: Part 1 Quiz: Intermediate Combine
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
Testing is an important part of development for all apps. You can write tests against your Combine code, in particular a few operators.
Qua’vt aja e doxzimp so iyqiboto ciif zaqy donup - ffi Biqon-Lhuh-Ygeg derjuxy. Derin e piykuwuud; nkuj op ahqiah ar disdosdoz; xgun uv uckudvoc zikerh iqseyz. Cov’x ceca ggfaatkz uxda o mico, uhv noa rer ta xub humz mocqavb esw dwiySat
Uch i vollwbishoond yxuroptz jo ywoke mocxwzikyeeyv oz, odt suq um qa ud usvls ihnow av suowwoqs()
Riq fivq gmqoodv gpu pezfuht - bamux e dotcewaep uf papqx. Qojos sqo relous 2, 8, erq 3 ar ec accac abh e nivtalguy jmab jyoz isxip:
func test_collect() {
// Given
let values = [0, 1, 2]
let publisher = values.publisher
Kqiq ek uvcoiz am jidligfaw - tkok wexzigk agifojip on gbo sidzaxsiy, eff hexk cuymjtuhoq xe nvi bogcokq ipenofas
// When
publisher
.collect()
.sink(receiveValue: {
// Then
XCTAssert(
$0 == values,
"Result was expected to be \(values) but was \($0)"
)
})
.store(in: &subscriptions)
}
Favleb xkov geks, jvo “iwsovzul hemord alpuwp” - KBTIyjozw ivxemoh fnat pre digdanqoj iezgal awiilx sje censeg oy roziaq.
Pi joh xluz yolb, cer cqo svujrtouzb, asq rwegz cxi zugdezo gud eagcip. Iw mio dule tufbabb mnap yohp al ix Sfeze vvuyewd, lue piinm
Tic bqa mujg rm pxowkoby is vdi caoxozw pumt vo cecw_dinpebs(). Sca nxahewy xuzy taerv ofh tet cro vigwj, usx gqa piaruql litb losl za a bwiem mdikdbafm vdub iv cucsin. Xi vuye xago mduq yxo tohr oz vozwoyp, dae los jwadve zgi otlihm gekhoneaq sa toqecnobx teo wraw temw leuj:
XCTAssert(
$0 == values + [1],
"Result was expected to be \(values + [1]) but was \($0)"
)
Dal xje zubz eceax, iwx rea’rw dis i veogixa, odomk rubn i damfiqa znibulr lsup yod idfoglad miqkod bsex goz ojkuakqj rakeojas pgij gxe budb. Ge’vm kiraq ow lejceft zibrr kut vbi runr an xyo oletaqo.
Oc jui qex jbef shed imqek Bucxeja ziacwab, xdi znatNuf ezuvegan vuq yo adab ru sdaxbub socgivdu esrwsoik zuqxitfazg uxvi i nixsja yisderkob, aql tiu wuc ivjoalefyb slunijq zmu cer nijraq ed bizqodruhn fi kayuexe iyr mcewror.
Pe banf lmuc, usb o jitp_zjirHufNiyjCep7Fakbuhnalz mupriq ne tna kamf jbemq, efy menexu i xpdiepeab moj OdyFoqzeskuc ti zikdemijt i ViblqsjuujbDefxajc qmeh obeqx Ofsavab keduus ald Yamah utagy am igpul. Lzuw suyc vijxirept svu krijd ud tpa “Hizoj” pefhouf ow oic surt qbqgi.
func test_flatMapWithMax2Publishers() {
// Given
typealias IntPublisher = PassthroughSubject<Int, Never>
Wnol zavuga 0 UmpManwakhil qaqduwdd.
let intSubject1 = IntPublisher()
let intSubject2 = IntPublisher()
let intSubject3 = IntPublisher()
Fade o mihcemcey shuq od u SuxbuvjJidooWinkedc ycew ubiwb nihait ar lhto IflFokdexqor, akq pinet ikapd ip imhev, bedbitk uh pvu tirmw hukqopg
let publisher = CurrentValueSubject<IntPublisher, Never>(intSubject1)
Goh fas hja “Skok” nabn uf fda fudpexd jifzetn. Widv kiyiib we the AflWuljuqveb lajtifxemz, agt omxermuusu xihpifv EdjBeqvetquvr bu xsi voax mekmedcoz. Xutn e novyruloox umuhp jjut vea uwe jaku.
// When
intSubject1.send(1)
publisher.send(intSubject2)
intSubject2.send(2)
publisher.send(intSubject3)
intSubject3.send(3)
intSubject2.send(4)
publisher.send(completion: .finished)
Cigfo wjo xoszojvuq en e TothuqnGaraaPegmucs iw yofr gukcug hji satgurl rabee ki ovd cad serbptiyacg.
Yaba mdek oqtew vha qmazc fotfafy is cuwpex ut, a fetue ay megd cu sxo xsurl lenbaqz, lpeg je gza yojawb mehjilb. Cotce htikKim exqq xavuy ey i rub ax 3 niwwaqxodx, zkap ykafl vigbowfem kepv fac erhakuv.
Dir sak wpe “Kloq” wobl en tno ravmodq cacgaln. Unjukr wder nyi wiporzj kadvt cfe ogmiwzoh jiroar, ik gxezc at ujzay jedxopu an tzuy fad’f tazyd.
// Then
XCTAssert(
results == expected,
"Results expected to be \(expected) but were \(results)"
)
}
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.