# Faster Ruby Testing: Only Test What Matters

DevFeed: [Faster Ruby Testing: Only Test What Matters](<https://devfeed.tech/articles/faster-ruby-testing-only-test-what-matters-21035.md>)

Original publisher: [Read original article](<https://jakeyesbeck.com/2015/08/02/test-only-what-matters/>)

Published: 2015-08-02T12:00:00Z

Content type: tutorial

Language: en

Sources: [Jake Yesbeck](<https://devfeed.tech/sources/jake-yesbeck.md>)

Topics: [Testing](<https://devfeed.tech/topics/testing.md>), [RSpec](<https://devfeed.tech/topics/rspec.md>), [Rails](<https://devfeed.tech/topics/rails.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>), [Database](<https://devfeed.tech/topics/database.md>), [Development](<https://devfeed.tech/topics/development.md>)

Tags: [activerecord](<https://devfeed.tech/tags/activerecord.md>), [bugs](<https://devfeed.tech/tags/bugs.md>), [code](<https://devfeed.tech/tags/code.md>), [database](<https://devfeed.tech/tags/database.md>), [development](<https://devfeed.tech/tags/development.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [ruby-on-rails](<https://devfeed.tech/tags/ruby-on-rails.md>), [speed](<https://devfeed.tech/tags/speed.md>), [testing](<https://devfeed.tech/tags/testing.md>), [tests](<https://devfeed.tech/tags/tests.md>)

## AI overview

This article explains how to make Ruby on Rails tests faster by keeping each test within the responsibility of the method under test. Using RSpec examples, it recommends avoiding unnecessary database writes and asserting method behavior directly when persistence is handled elsewhere.

## Source excerpt

Automated testing is important. Fast, exhaustive automated testing is even more important. Tests are responsible for ensuring the code you spend hours creating actually works. A great test suite can be a safeguard against bugs, a directional guide towards extending the code, and an accurate measurement of the codebase's health. The key to writing good tests is understanding where pieces of responsibility begin and end. Maintaining small concise automated tests can make all the difference. Speed is King Aside from the general importance of short feedback cycles, a fast test suite will greatly improve your work-flow and mood. Conversely, a slow test suite can make your development cycle a living nightmare. How many unfortunate software artisans have dealt with something like: Bleh! What a horrible thing to deal with. What, am I supposed to just wait 5 whole minutes every time I want to test my system? A test suite this slow is simply unacceptable. So what could be causing this slowness? More than likely, a number of tests in this application are doing too much. To try and replicate and solve this problem, let's assume we have a Ruby on Rails application we test using RSpec. Testing Database Models is Slow We can assume that there exists a very important class called MyAwesomeClass: class MyAwesomeClass < ActiveRecord::Base def assign_and_save! saved_on_the_weekend = weekend? self.save! end def weekend? Time.now.saturday? || Time.now.sunday? end end If we wanted to test that the assign_and_save! method works correctly, we might write a test like: describe '#assign_and_save!' do it 'assigns and saves' do awesome_test = MyAwesomeClass.new expect do awesome_test.assign_and_save! end.to change(MyAwesomeClass, :count).by(1) expect(awesome_test.saved_on_the_weekend).to_not be_nil end end These tests make sure that the model is saved correctly to the database and a value is assigned to saved_on_the_weekend. However, the test code is overstepping the boundaries of the method i