LinuxCommandLibrary

rspec

Run and execute RSpec tests

TLDR

Initialize an .rspec configuration and a spec helper file

$ rspec --init
copy

Run all tests
$ rspec
copy

Run a specific directory of tests
$ rspec [path/to/directory]
copy

Run one or more test files
$ rspec [path/to/file1 path/to/file2 ...]
copy

Run a specific test in a file (e.g. the test starts on line 83)
$ rspec [path/to/file]:[83]
copy

Run specs with a specific seed
$ rspec --seed [seed_number]
copy

SYNOPSIS

rspec [OPTIONS] [FILES_OR_DIRECTORIES]

Examples:
rspec (Runs all specs in the default `spec` directory)
rspec spec/models/user_spec.rb (Runs a specific spec file)
rspec -f d (Runs all specs with documentation formatter)
rspec --init (Initializes RSpec in the current directory)

PARAMETERS

-f, --format FORMATTER
    Specifies the output formatter (e.g., 'progress', 'documentation', 'html').

-d, --dry-run
    Don't run examples, just show what would run.

-e, --example PATTERN
    Runs examples whose full descriptions match the given pattern.

-t, --tag TAG
    Runs examples tagged with the specified tag (e.g., `--tag focus`).

--require FILE
    Requires a file before running tests (useful for custom helpers).

--init
    Initializes RSpec in the current directory by creating necessary files and folders.

--color, --no-color
    Enables or disables color output in the console.

-b, --backtrace
    Shows the full backtrace for failures.

--fail-fast
    Aborts the test run after the first failure.

-l, --line_number LINE
    Runs the example located at a specific line number within a file.

--order RAND
    Randomizes the order of examples for better test isolation (e.g., 'rand', 'defined').

--help
    Displays the help message and exits.

DESCRIPTION

RSpec is a powerful testing framework for the Ruby programming language, widely adopted for Behavior-Driven Development (BDD). It provides a structured and readable way to describe the expected behavior of Ruby applications through executable examples. Unlike traditional unit testing frameworks, RSpec encourages writing "specs" that read like natural language, making tests more understandable to a broader audience, including business stakeholders.

The framework comprises several core components: rspec-core (the test runner), rspec-expectations (for expressing assertions), and rspec-mocks (for creating test doubles like stubs and mocks). Developers define system behaviors using describe blocks for components and it blocks for specific examples, utilizing expect to assert outcomes. When executed, rspec runs these examples and reports successes or failures, providing immediate feedback during development and helping ensure code quality. It's typically installed as a Ruby gem within a project's dependency management system.

CAVEATS

RSpec is not a built-in Linux utility. It requires the Ruby programming language and the RSpec gem to be installed on the system. Its usage is primarily within Ruby software development contexts.

<B>BDD PHILOSOPHY</B>

RSpec is built upon the principles of Behavior-Driven Development (BDD), a software development methodology that extends Test-Driven Development (TDD) by focusing on describing the desired behavior of software from the perspective of its users or stakeholders. This approach leads to more readable, maintainable, and collaborative test suites that often serve as living documentation for the application.

<B>PROJECT INTEGRATION</B>

For Ruby projects, RSpec is typically added as a dependency in the project's Gemfile and installed via `bundle install`. Running `rspec --init` in a project directory sets up the necessary structure, including a `spec/` directory for test files and a `spec_helper.rb` for shared configuration and setup logic.

HISTORY

RSpec emerged around 2005-2006, influenced by other BDD tools and testing frameworks like JMock and NUnit. It quickly gained traction in the Ruby community for its expressive syntax and focus on behavior. Key figures in its development include Steven Baker, David Chelimsky, and Myron Marston. The framework has undergone several major version updates, continually refining its API and performance, solidifying its position as the dominant testing tool for Ruby on Rails and other Ruby applications. Its popularity significantly contributed to the adoption of BDD principles in agile development.

SEE ALSO

ruby(1), gem(1), bundle(1), rake(1)

Copied to clipboard