<?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" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jerrel Mitchell on Medium]]></title>
        <description><![CDATA[Stories by Jerrel Mitchell on Medium]]></description>
        <link>https://medium.com/@kelveaux90?source=rss-6c0351b57eb7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*s1S0j-45xFONbdRH.</url>
            <title>Stories by Jerrel Mitchell on Medium</title>
            <link>https://medium.com/@kelveaux90?source=rss-6c0351b57eb7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 22:12:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kelveaux90/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Testing Types in Rails 4.2 (with Capybara)]]></title>
            <link>https://medium.com/@kelveaux90/tdd-test-driven-development-is-one-approach-to-developing-an-web-application-but-not-completely-e1cb50495a2b?source=rss-6c0351b57eb7------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1cb50495a2b</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Jerrel Mitchell]]></dc:creator>
            <pubDate>Wed, 06 Jun 2018 21:33:36 GMT</pubDate>
            <atom:updated>2018-06-07T02:10:00.514Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*g2-_Rgd3lEvPz3PjcrDXsg.jpeg" /><figcaption>womp womp</figcaption></figure><p>TDD (Test Driven Development) is one approach to developing a web application, but not completely accepted by all in the community. TDD will have you write tests first before writing code, so that you can make your tests pass. Many have concerns or qualms with several aspects of testing, including the amount of time it takes just to set them up! With this guide, I hope I can make testing seem a little less daunting and a little more fun!</p><h3>Why Test</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/269/1*bSLciQXol6kJ8UPzitusoA.jpeg" /><figcaption>We’ve all in our coding explorations, at one point or another, given this a thought.</figcaption></figure><p>Testing will help you develop behaviors within your app. Like when you write a test where your model is validating the presence of a username, then realize you need to include validations for a password or add a password confirmation field.</p><p>Another way to build your tests out is to write a test after you find an issue or bug in your application. This helps ensure that the fix works and won’t cause problems for your application in the future.</p><h3>Testing Frameworks</h3><p>There are default gems/libraries for Rails, and there are many options out there to choose from which are sometimes more used than the defaults. MiniTest comes with Rails, but I prefer RSpec because it focuses on readability, and can be written to be more specific to other tests. It uses words such as ‘describes’ and ‘it’ when creating tests/groups of tests. Example:</p><pre>describe “the test” do<br>  it “does this or the other thing” do<br>    expected = example.setup</pre><pre>    expect(expected).to eq(example.setup)<br>  end<br>end&#39;</pre><p>This is a very contrived example showing a basic set up of an Rspec test, which is slightly different to other test suites such as MiniTest.</p><p><strong>RSpec/Capybara Feature Tests &amp; Integration Tests</strong></p><p>A great addition to RSpec is Capybara, which you can use while you build in integration tests to set up workflow. Capybara plugs right into RSpec, allowing you to use the same syntax to write tests! Example:</p><pre>...</pre><pre>describe “the sign in process” do</pre><pre> before(:each) do<br>   User.create(email: ‘user@example.com’, password: ‘password’)<br> end</pre><pre>  it “signs me in” do</pre><pre>     visit sessions_path</pre><pre>     within(“#css_class”) do<br>       fill_in :user_email, with: ‘user@example.com’<br>       fill_in :user_password, with: ‘password’<br>     end</pre><pre>     click_button ‘Sign in’</pre><pre>     expect(page).to have_content ‘Success’<br>   end</pre><pre>...</pre><pre>end</pre><p>This example shows how you might create a feature test (more on that later) in Rspec with the help of Capybara methods.</p><h3>Testing Types</h3><p>There are three(3) main types of tests in a Rails application. Model(Unit), Controller, and View tests. You can also use Feature Tests, which function as an end-to-end walkthrough test of your application (and for the examples today you can think of our Feature test as a large view test!).</p><p>Each test type has a place in the testing world and should be considered equally important (especially if you enjoy testing like me!).</p><h3>Model/Unit Tests</h3><p>Model specs are typically the simplest test types you will experience in Rails. With these, you will be testing CRUD(Create/Read/Update/Delete) operations on the model object. They can also test validations and/or associations. Model tests will (usually) run quickly.</p><pre>class User &lt; ApplicationRecord<br>  validates :username, uniqueness: true<br>  validates :username, presence: true</pre><pre>...</pre><pre>  describe &#39;validations&#39; do<br>    it {should validate_presence_of(:username)}<br>    it {should validate_uniqueness_of(:username)}<br>  end</pre><pre>...</pre><pre>end</pre><p>The provided example shows Shoulda Matchers on the model level to test that a model is validating a usernames presence and uniqueness.</p><h3>Controller Tests</h3><p>Controller tests will be what you use when testing conditional logic. With tests based on request type, user type, success/failure of Models, etc</p><p>Controller specs can be slower than Model specs, mostly because their use of the request/response cycle. They’re still considerably faster than feature specs. This makes it a great place to do the bulk of your testing.</p><pre>describe &#39;admin&#39; do<br>  context &#39;admin visits /admin/dashboard&#39; do<br>    it &#39;should experience a valid path&#39; do<br>      admin=User.create(username: &quot;Bob&quot;, password: &quot;123&quot;, role: 1) </pre><pre>      allow_any_instance_of(ApplicationController)<br>        .to receive(:current_user)<br>        .and_return(admin)</pre><pre>      visit &#39;/admin/dashboard&#39;</pre><pre>      expect(current_path).to eq(&#39;/admin/dashboard&#39;)<br>    end<br>  end<br>end</pre><p>In this test file, you can see that we’re testing that an admin user can visit an admin dashboard successfully.</p><p>These contrived tests aren’t complex and are small in scope to give you a taste of how quick to set up controller testing can be. In a real world controller file, you would be testing all actions that are involved with a controller action. Such as testing its behavior and rendering, and also what’s within each branch of the action; including success or failure of a database transaction.</p><h3>Feature Tests</h3><p>Feature tests are the FUN way test your entire application at once (or large parts of behavior). Using RSpec and Capybara, you have the ability to write easy to read tests that will walk through your app as if you were an actual user.</p><pre>describe &#39;As admin user&#39; do</pre><pre>...</pre><pre>  describe &#39;can delete a trip from the index page&#39; do<br>    it &#39;successfully deletes the trip&#39; do</pre><pre>      admin = User.create!(username:&#39;Bob&#39;, password:&#39;123&#39;, role:1)</pre><pre>      allow_any_instance_of(ApplicationController)<br>        .to receive(:current_user)<br>        .and_return(admin)</pre><pre>      Trip.create!(duration: 30,<br>                   bike_id: 10,<br>                   subscription_type: &#39;Customer&#39;,<br>                   zip_code: 11234)</pre><pre>      visit trips_path</pre><pre>      expect(Trip.count).to eq(1)<br>      expect(page).to have_content(&#39;Customer&#39;)</pre><pre>      click_on &#39;Delete&#39;</pre><pre>      expect(Trip.count).to eq(0)<br>      expect(page).to_not have_content(&#39;Customer&#39;)<br>    end<br>...</pre><pre>  end</pre><pre>...</pre><pre>end</pre><p>This test shows an admin User being able to delete a Trip from a Trip index page.</p><p>Feature tests provide a cool and interactive way to test at a high level, just as a user would be interacting with your application. This does mean that these tests will run the slowest. (Which isn’t bad if you use the <a href="https://github.com/mattsears/nyan-cat-formatter"><strong><em>Nyan Cat</em></strong></a> gem!) But, if you’re the type that doesn’t enjoy life, or if you want your tests to finish quickly, you might like to know that because these tests typically run the slowest; you should use controller or model tests to check edge cases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*KRGiYWzY3a5_8mBf1CcbZw.gif" /><figcaption><em>The Nyan Cat Rspec Formatter for your terminal. It’s like minitest/pride on steroids.</em></figcaption></figure><p>Well, thanks for reading through my short walkthrough of different types of testing in Rails! Testing really can be fun if you dig into the different types of testing and like to attempt to plan through what you’re building. Hope you found this guide at least a little helpful.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1cb50495a2b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>