<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ RxSwift - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ RxSwift - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Mon, 25 May 2026 20:15:59 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/rxswift/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to run tests in RxSwift ]]>
                </title>
                <description>
                    <![CDATA[ By Navdeep Singh RxTest and RxBlocking are part of the RxSwift repository. They are made available via separate pods, however, and so require separate imports. RxTest provides useful additions for testing Rx code. It includes TestScheduler, which is ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/testing-in-rxswift-2b6eeaeaf432/</link>
                <guid isPermaLink="false">66c360719539e75f2cc24a27</guid>
                
                    <category>
                        <![CDATA[ coding ]]>
                    </category>
                
                    <category>
                        <![CDATA[ iOS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Reactive Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ RxSwift ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 19 Mar 2018 04:15:05 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*ASJBAG5pLUIdEpVU4_RM2A.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Navdeep Singh</p>
<p><strong>RxTest</strong> and <strong>RxBlocking</strong> are part of the RxSwift repository. They are made available via separate pods, however, and so require separate imports.</p>
<p><strong>RxTest</strong> provides useful additions for testing Rx code. It includes <strong>TestScheduler</strong>, which is a virtual time scheduler, and provides methods for adding events at precise time intervals.</p>
<p><strong>RxBlocking</strong>, on the other hand, enables you to convert a regular Observable sequence to a blocking observable, which blocks the thread it’s running on until the observable sequence completes or a specified timeout is reached. This makes testing asynchronous operations much easier.</p>
<p>Let’s look at each one now.</p>
<h3 id="heading-rxtest">RxTest</h3>
<p>As described above, <strong>RxTest</strong> is part of the same repository as <strong>RxSwift.</strong> There is one more thing to know about <strong>RxTest</strong> before we dive into some RxTesting: RxTest exposes two types of Observables for testing purposes.</p>
<ul>
<li>HotObservables</li>
<li>ColdObservables</li>
</ul>
<p>HotObservables replay events at specified times using a test scheduler, regardless of whether there are any subscribers.</p>
<p>ColdObservables work more like regular Observables, replaying their elements to their new subscribers upon subscription.</p>
<h3 id="heading-rxblocking">RxBlocking</h3>
<p>If you are familiar with expectations in <strong>XCTest</strong>, you will know that it’s another way to test asynchronous operations. Using RxBlocking just happens to be way easier. Let’s start with a small implementation so we can see how to take advantage of this library while testing asynchronous operations.</p>
<h3 id="heading-testing-with-rxblocking">Testing with RxBlocking</h3>
<p>We will start a new test and create an Observable of 10, 20, and 30, as follows:</p>
<pre><code>func testBlocking(){        <span class="hljs-keyword">let</span> observableToTest = Observable.of(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>)    }
</code></pre><p>Now we will define the result as equal to calling toBlocking() on the observable we created:</p>
<pre><code><span class="hljs-keyword">let</span> result = observableToTest.toBlocking()
</code></pre><p><strong>toBlocking()</strong> returns a blocking Observable to a straight array, as you can see here:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1F4TF7RZ2FlBgcynP9tWUi9UqQBtLCCy6YSg" alt="Image" width="488" height="187" loading="lazy"></p>
<p>We will need to use the first method if we want to discover which is a throwing method. So we will wrap it in a do catch statement, and then we will add an <strong>AssertEquals</strong> statement if it is successful, as follows:</p>
<pre><code>func testBlocking(){        <span class="hljs-keyword">let</span> observableToTest = Observable.of(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>)        <span class="hljs-keyword">do</span>{            <span class="hljs-keyword">let</span> result = <span class="hljs-keyword">try</span> observableToTest.toBlocking().first()            XCTAssertEqual(result, <span class="hljs-number">10</span>)        } <span class="hljs-keyword">catch</span> {        }    }
</code></pre><p>Alternatively, an Assert fails if it’s not this:</p>
<pre><code><span class="hljs-keyword">do</span>{            <span class="hljs-keyword">let</span> result = <span class="hljs-keyword">try</span> observableToTest.toBlocking().first()            XCTAssertEqual(result, <span class="hljs-number">10</span>)        } <span class="hljs-keyword">catch</span> {            XCTFail(error.localizedDescription)        }
</code></pre><p>That’s it! Let’s run the test, and you will see that the test passes. We can simplify this test with just two lines of code by forcing the try.</p>
<p>Again, this is more acceptable on test than production code. We will comment out the do catch statement and then write the assert equals in a single line, as follows:</p>
<pre><code>XCTAssertEqual(<span class="hljs-keyword">try</span>! observableToTest.toBlocking().first(), <span class="hljs-number">10</span>)
</code></pre><p>Rerun the test, and you will see that the test passes once again. The overall code with comments looks like this:</p>
<pre><code>func testBlocking(){        <span class="hljs-keyword">let</span> observableToTest = Observable.of(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>)<span class="hljs-comment">//        do{//            let result = try observableToTest.toBlocking().first()//            XCTAssertEqual(result, 10)//        } catch {//            XCTFail(error.localizedDescription)//        }        XCTAssertEqual(try! observableToTest.toBlocking().first(), 10)    }</span>
</code></pre><p><strong>How’s that for succinct?</strong> Truth be told, that Observable sequence would actually be synchronous already if we printed emitted elements in a subscription to it followed by a marker. The marker will be printed after the subscription’s completed event.</p>
<p>To test an actual asynchronous operation, we will write one more test. This time, we will use a concurrent scheduler on a background thread, as follows:</p>
<pre><code>func testAsynchronousToArry(){        <span class="hljs-keyword">let</span> scheduler = ConcurrentDispatchQueueScheduler(qos: .background)    }
</code></pre><p>Now, we will create an Observable of the simple sequence of integers. We will use map to double each value, as follows:</p>
<pre><code><span class="hljs-keyword">let</span> intObservbale = Observable.of(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>)            .map{ $<span class="hljs-number">0</span> * <span class="hljs-number">2</span> }
</code></pre><p>Then, we will subscribe on the scheduler:</p>
<pre><code><span class="hljs-keyword">let</span> intObservbale = Observable.of(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>)            .map{ $<span class="hljs-number">0</span> * <span class="hljs-number">2</span> }            .subscribeOn(scheduler)
</code></pre><p>Now we will write a do catch statement that is similar to the last test and calls toBlocking on the Observable, which should be observed on the main scheduler, as follows:</p>
<pre><code><span class="hljs-keyword">do</span>{   <span class="hljs-keyword">let</span> result = <span class="hljs-keyword">try</span> intObservbale.observeOn(MainScheduler.instance).toBlocking().toArray()   } <span class="hljs-keyword">catch</span> {   }
</code></pre><p>Then, we will add the same assertions as the previous example:</p>
<pre><code><span class="hljs-keyword">do</span>{   <span class="hljs-keyword">let</span> result = <span class="hljs-keyword">try</span> intObservbale.observeOn(MainScheduler.instance).toBlocking().toArray()            XCTAssertEqual(result, [<span class="hljs-number">20</span>, <span class="hljs-number">40</span>, <span class="hljs-number">60</span>])        } <span class="hljs-keyword">catch</span> {            XCTFail(error.localizedDescription)        }
</code></pre><p>Now we will run the test, and you will note that it passes with the green check mark in the gutter.</p>
<p>Note that the marker is printed before the emitted elements in the console, as shown:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/699d2sSCtcHgNblX68UK9VKzUBF-f1aTW8r0" alt="Image" width="633" height="95" loading="lazy"></p>
<p>This is because these operations were executed asynchronously.</p>
<p>For other updates you can follow me on Twitter on my twitter handle @NavRudraSambyal</p>
<p>To work with examples of hot and cold observables, you can find the link to my book <a target="_blank" href="https://www.amazon.com/Reactive-Programming-Swift-easy-maintain-ebook/dp/B078MHNSL1/ref=asap_bc?ie=UTF8">Reactive programming in Swift 4</a></p>
<p>Thanks for reading, please share it if you found it useful :)</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
