百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rspec-rails-examples

> 后端框架
开源

RSpec 速查表与 Rails 应用: 了解如何从模型代码库中专业地测试 Rails 应用

2.1K stars0 点赞1 次浏览
访问官网GitHub

工具介绍

RSpec 速查表与 Rails 应用: 了解如何从模型代码库中专业地测试 Rails 应用

RSpec Rails Examples

An RSpec cheatsheet in the form of a Rails app. Learn how to expertly test Rails apps from a model codebase

A small yet comprehensive reference for developers who want to know how to test Rails apps using RSpec.

Here you'll find in-depth examples with detailed documentation explaining how to test with RSpec and related testing gems, which you can then apply to your own projects.

This application was originally written for the benefit of the developers I coach, who've found it a useful memory aid and catalyst for when they're learning RSpec. Now I'd like to get feedback from the wider community.

The repo contains examples of various spec types such as feature, mailer, and model. See the spec/ directory for all the example specs and types.

In the README below, you'll find links to some of the most useful cheatsheets and API documentation available for RSpec users.

See the well-commented files in the spec/support directory for walkthroughs on how to configure popular testing gems, such as DatabaseCleaner, Capybara, and FactoryGirl.

Hopefully this will be of help to those of you learning RSpec and Rails. If there's anything missing you'd like to see covered in the project, please submit your request via the issue tracker, I'd be happy to help — Eliot Sykes

PS. Interested in growing your skills and supporting this project? Learn with the TDD Masterclass, get Test Coverage First Aid for your app, or grow with one-to-one coaching for Rails developers.


  • Support Configuration
  • Run Specs in a Random Order
  • Testing Production Errors
  • Testing Rake Tasks with RSpec
  • Pry-rescue debugging
  • Time Travel Examples
  • ActiveJob Examples
  • Database Cleaner Examples
  • Factory Girl Examples
  • VCR Examples
  • Capybara Examples
  • Puffing Billy Examples
  • Shoulda-Matchers Examples
  • Email-Spec Examples
  • Devise Examples
  • Custom Matchers
  • RSpec-Expectations Docs
  • RSpec-Mocks Specs & Docs
  • RSpec-Rails
    • Matchers
    • Generators
    • Feature Specs & Docs
    • API Request Specs, Docs, & Helpers
    • Mailer Specs & Docs
    • Controller Specs & Docs
    • View Specs & Docs
    • Helper Specs & Docs
    • Routing Specs & Docs
  • Validator Specs
  • Enable Spring for RSpec
  • Automated Continuous Integration with Travis CI
  • Contributors

Support Configuration

The tests rely on this configuration being uncommented in spec/rails_helper.rb, you probably want it uncommented in your app too:

# Loads `.rb` files in `spec/support` and its subdirectories:
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

(The rspec-rails installer generates this line, but it will be commented out.)

Run Specs in a Random Order

In a dependable, repeatable automated test suite, data stores (such as database, job queues, and sent email on ActionMailer::Base.deliveries) should return to a consistent state between each spec, regardless of the order specs are run in.

For a maintainable, predictable test suite, one spec should not set up data (e.g. creating users) needed by a later spec to pass. Each spec should look after its own test data and clear up after itself. (NB. If there is reference data that all tests need, such as populating a countries table, then this can go in db/seeds.rb and be run once before the entire suite).

The specs run in a random order each time the test suite is run. This helps prevent the introduction of run order and test data dependencies between tests, which are best avoided.

Random order test runs are configured using the config.order = :random and Kernel.srand config.seed options in spec/spec_helper.rb.

Testing Production Errors

When errors are raised, the Rails test environment may not behave as in production. The test environment may mask the actual error response you want to test. To help with this, you can disable test environment exception handling temporarily, spec/support/error_responses.rb provides respond_without_detailed_exceptions. See it in use in spec/api/v1/token_spec.rb to provide production-like error responses in the test environment.

Testing Rake Tasks with RSpec

RSpec testing Rake task configuration and example:

  • spec/support/tasks.rb essential to load Rake tasks before specs run
  • spec/tasks/subscription_tasks_spec.rb

Pry-rescue debugging

pry-rescue can be used to debug failing specs, by opening pry's debugger whenever a test failure is encountered. For setup and usage see pry-rescue's README.

Time Travel Examples

ActiveSupport::Testing::TimeHelpers provides helpers for manipulating and freezing the current time reported within tests. These methods are often enough to replace the time-related testing methods that the timecop gem is used for.

TimeHelpers configuration how-to and examples:

  • spec/support/time_helpers.rb
  • spec/models/subscription_spec.rb
  • spec/tasks/subscription_tasks_spec.rb
  • travel_to example: spec/models/subscription_spec.rb
  • ActiveSupport::Testing::TimeHelpers API documentation

ActiveJob Examples

ActiveJob::TestHelper provides help to test ActiveJob jobs.

ActiveJob::TestHelper configuration how-to and examples:

  • spec/support/job_helpers.rb
  • spec/jobs/headline_scraper_job_spec.rb
  • ActiveJob::TestHelper API documentation

Database Cleaner Examples

Database Cleaner is a set of strategies for cleaning your database in Ruby, to ensure a consistent environment for each test run.

Database Cleaner configuration how-to:

  • spec/support/database_cleaner.rb

Factory Girl Examples

Factory Girl is a library to help setup test data. factory_girl_rails integrates Factory Girl with Rails.

Factory Girl configuration how-to and examples:

  • spec/support/factory_girl.rb
  • spec/factories
  • spec/factories/users.rb
  • spec/models/subscription_spec.rb
  • spec/tasks/subscription_tasks_spec.rb
  • spec/features/user_login_and_logout_spec.rb

VCR Examples

VCR records your test suite's HTTP interactions and replays them during future test runs. Your tests can run independent of a connection to external URLs. These HTTP interactions are stored in cassette files.

VCR configuration how-to and examples:

  • spec/support/vcr.rb
  • spec/jobs/headline_scraper_job_spec.rb
  • Cassette files in spec/support/http_cache/vcr
  • VCR Relish docs
  • VCR API docs

Capybara Examples

Capybara helps you write feature specs that interact with your app's UI as a user does with a browser.

Capybara configuration how-to and examples:

  • spec/support/capybara.rb
  • spec/features/home_page_spec.rb
  • spec/features/subscribe_to_newsletter_spec.rb
  • spec/features/user_login_and_logout_spec.rb
  • spec/features/user_registers_spec.rb
  • Capybara cheatsheet
  • Capybara matchers

Puffing Billy Examples

Puffing Billy is like VCR for browsers used by feature specs. Puffing Billy is a HTTP proxy between your browser and external sites, including 3rd party JavaScript. If your app depends on JavaScript hosted on another site, then Puffing Billy will keep a copy of that JavaScript and serve it from a local web server during testing. This means tests dependent on that JavaScript will carry on working even if the original host cannot be connected to.

If you need to debug Puffing Billy, refer to its output in log/test.log.

Puffing Billy configuration how-to and examples:

  • spec/support/puffing_billy.rb
  • spec/features/share_page_spec.rb
  • Cache options
  • Cached responses in spec/support/http_cache/billy

Shoulda-Matchers Examples

Shoulda-matchers make light work of model specs.

shoulda-matchers configuration how-to and examples:

  • spec/support/shoulda_matchers.rb
  • spec/models/subscription_spec.rb

Email-Spec Examples

The "Subscribe to newsletter" feature was developed with help from email_spec

email_spec configuration how-to and examples:

  • spec/support/email_spec.rb
  • spec/jobs/headline_scraper_job_spec.rb
  • spec/mailers/news_mailer_spec.rb
  • spec/mailers/subscription_mailer_spec.rb
  • spec/features/subscribe_to_newsletter_spec.rb
  • spec/features/user_registers_spec.rb
  • EmailSpec::Helpers API documentation
  • EmailSpec::Matchers API documentation

Devise Examples

Specs testing registration, sign-in, and other user authentication features provided by Devise:

  • spec/features/user_login_and_logout_spec.rb
  • [spec

GitHub Issues· 0 开放

在 GitHub 查看全部

暂无开放 Issues,或尚未同步最近议题。

核心特点

  • •Support Configuration
  • •Run Specs in a Random Order
  • •Testing Production Errors
  • •Testing Rake Tasks with RSpec
  • •Pry-rescue debugging
  • •Time Travel Examples
  • •ActiveJob Examples
  • •Database Cleaner Examples
  • •Factory Girl Examples
  • •VCR Examples

> 标签

Rubyfactory-girllearning-rspecrailsrails-application

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架