<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alan Ferreira</title>
    <description>The latest articles on DEV Community by Alan Ferreira (@alanmaik).</description>
    <link>https://dev.to/alanmaik</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1330008%2F0d7ede33-2590-4e69-bfdf-1a57d88af1fd.jpeg</url>
      <title>DEV Community: Alan Ferreira</title>
      <link>https://dev.to/alanmaik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alanmaik"/>
    <language>en</language>
    <item>
      <title>Creating Better Test Scenarios in Ruby on Rails</title>
      <dc:creator>Alan Ferreira</dc:creator>
      <pubDate>Wed, 17 Jul 2024 22:03:23 +0000</pubDate>
      <link>https://dev.to/alanmaik/creating-better-test-scenarios-in-ruby-on-rails-1hee</link>
      <guid>https://dev.to/alanmaik/creating-better-test-scenarios-in-ruby-on-rails-1hee</guid>
      <description>&lt;p&gt;Dev Tip: Enhance Your Test Scenarios in Rails!&lt;/p&gt;

&lt;p&gt;Hey devs! 👋 Today, I want to share some tips on creating more efficient test scenarios in Ruby on Rails. Testing our code is crucial to ensure the quality and robustness of our applications, so let's dive in!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Understand the Context of Your Tests&lt;/li&gt;
&lt;li&gt;Unit Tests: Focus on individual methods. Test logic in isolation.&lt;/li&gt;
&lt;li&gt;Integration Tests: Check how different parts of your system interact.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;System Tests: Evaluate the entire application, simulating the end-user experience.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use Factories Instead of Fixtures&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Factories: Tools like FactoryBot help create dynamic and flexible test data, avoiding issues with static and repetitive data.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Example with FactoryBot
FactoryBot.define do
  factory :user do
    name { "John Doe" }
    email { "john.doe@example.com" }
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Keep Tests Independent&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each test should be independent of others. Avoid dependencies to ensure that one test's failure doesn't affect the rest.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Utilize let and let! for Test Setup&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;let&lt;/code&gt;: Creates lazy-loaded variables, instantiated only when used.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;let&lt;/code&gt;!: Creates variables immediately, useful for setups needed before tests run.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let(:user) { create(:user) }
let!(:admin) { create(:user, admin: true) }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Write Clear and Descriptive Tests&lt;/li&gt;
&lt;li&gt;Name your tests clearly to describe what’s being tested. This makes it easier to understand what went wrong when a test fails.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;it "returns the full name of the user" do
  user = build(:user, first_name: "John", last_name: "Doe")
  expect(user.full_name).to eq("John Doe")
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Mocking and Stubbing with RSpec&lt;/li&gt;
&lt;li&gt;Use mocks and stubs to isolate parts of your code and test specific behaviors without relying on external implementations.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;allow(User).to receive(:find).with(1).and_return(user)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Test with Realistic Data&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whenever possible, test with data that represents real usage scenarios. This increases confidence that the code will work as expected in production.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use before and after for Setup and Cleanup&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;before&lt;/code&gt; and &lt;code&gt;after&lt;/code&gt; blocks help set up the test environment and clean up data or states after each test.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;before do
  # Setup before each test
end

after do
  # Cleanup after each test
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>ruby</category>
      <category>rails</category>
      <category>tdd</category>
      <category>codequality</category>
    </item>
    <item>
      <title>How to save time when run RSpec tests</title>
      <dc:creator>Alan Ferreira</dc:creator>
      <pubDate>Sat, 06 Jul 2024 19:39:19 +0000</pubDate>
      <link>https://dev.to/alanmaik/how-to-save-time-when-run-rspec-tests-10ld</link>
      <guid>https://dev.to/alanmaik/how-to-save-time-when-run-rspec-tests-10ld</guid>
      <description>&lt;p&gt;&lt;strong&gt;two tips that help a lot to save time when running test suites on Rails with RSpec.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Reduce Devise.stretches
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: &lt;em&gt;This tip is useful if you are using Devise as an &lt;br&gt;
  authentication tool.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In your spec/test.rb file, add the following line:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Devise.stretches = 1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When using Devise, the cost value is set by a class variable called stretches, with a default value of 11. It specifies the number of times the password is hashed. By setting this value lower, you make the hash algorithm less costly and time-consuming, saving time in the test suite as it's unnecessary to have such a secure password in our testing environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Increase log level in the test environment
&lt;/h2&gt;

&lt;p&gt;In your spec/test.rb file, add the following line:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Rails.logger.level = 4&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Rails logs everything that happens in your test environment by default to "log/test.log". By increasing the logger level, you reduce IO during your tests. The downside is that if a test fails, nothing will be logged. In such cases, simply comment out the above configuration option and rerun your tests.&lt;/p&gt;

&lt;p&gt;These two tips already help you save a lot of time when running the test suite with RSpec. Hope this helps!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>tdd</category>
      <category>test</category>
      <category>rspec</category>
    </item>
  </channel>
</rss>
