# A Few RSpec Helpful Hints

DevFeed: [A Few RSpec Helpful Hints](<https://devfeed.tech/articles/a-few-rspec-helpful-hints-21074.md>)

Original publisher: [Read original article](<https://jakeyesbeck.com/2017/07/12/a-few-rspec-helpful-hints/>)

Published: 2017-07-12T12:00:00Z

Content type: tutorial

Language: en

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

Topics: [RSpec](<https://devfeed.tech/topics/rspec.md>), [Testing](<https://devfeed.tech/topics/testing.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [developer](<https://devfeed.tech/tags/developer.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [testing](<https://devfeed.tech/tags/testing.md>), [tests](<https://devfeed.tech/tags/tests.md>)

## AI overview

This tutorial presents practical RSpec techniques for writing Ruby tests that are readable, reusable, and easier to maintain. It covers subject and let variables, loose expectations, argument matchers, and hash matchers.

## Source excerpt

Two main frameworks dominate the Ruby testing world: Rspec and MiniTest. RSpec is a very expressive testing framework with many great features and helpers to make tests readable. When writing RSpec tests, here are just a few not so obvious hints that could make tests even easier to write, read, and maintain. Assuming a system exists with Books and Authors, let's utilize these hints to make testing easy. class Book attr_reader :title, :genre def initialize(title, genre) @title = title @genre = genre end end class Author attr_reader :books def initialize(name, books) @name = name @books = Array(books) end def has_written_a_book? !books.empty? end end subject and let Variables A great way to keep specs DRY and readable are via subject and let variable declarations. For example, if we want to assert an Author has a name, a test without let and subject variables might look something like: describe Author do before do @book_genre = 'Historical Fiction' @book_title = 'A Tale of Two Cities' @book = Book.new(@book_genre, @book_title) @author_name = 'Charles Dickens' @author = Author.new(@author_name, [@book]) end describe '#name'do it 'has a name set' do expect(@author.name).to eq(@author_name) end end end While correct, additional tests asserting number of books, a different name, or other things about this Author could become very verbose. Instead, we can introduce subject and let variables to keep things DRY and reusable: describe Author do let(:book_genre) { 'Historical Fiction' } let(:book_title) { 'A Tale of Two Cities' } let(:book) { Book.new(book_genre, book_title) } let(:book_array) { [book] } let(:author_name) { 'Charles Dickens' } subject { Author.new(author_name, book_array) } describe '#name'do it 'has a name set' do expect(subject.name).to eq(author_name) end end describe '#books' do context 'with books' do it 'has books set' do expect(subject.books).to eq(book_array) end end context 'without books' do context 'books variable is nil' do let(:book_array) { nil }