RSpec is an open source Behavior-Driven Development (BDD) ruby package that lets you build a specification alongside your software. BDD is an approach to software development that combines Test-Driven Development (TDD), Domain Driven Design and Acceptance Test-Driven Planning. RSpec helps you do the TDD part of that equation, focusing on the documentation and design aspects of TDD.
You start by describing what you want, using #describe to define each method passing the method’s name as the argument. For class method Rspec prefix a “.” to the name, and for instance level Rspec uses a “#” prefix. This is similar to standard Ruby documentation practices. You define the #context to explain the different scenarios in which the method could be executed. Each #context describes the state of the world before execution of the method. You need a context for each execution path through a method. Finally, you specify the behavior with the objective of having only one behavior specified in each example.
Given a file named “sample_spec.rb” with:
describe "something" do it "does something" do end end
When I run `rspec sample_spec.rb -fn`
Then the output should contain:
something does something
RSpec Best Practices
* Don’t begin example names with the word “should”.
* Use the right matcher: avoid # before with a double negative
* Use the “before :all” block carefully
* Do not create hundreds of records for a particular spec
* Do not over-mock
* Test the edge cases
* Stop “spec_helper” from being loaded multiple times
* Run RSpec to confirm readability
* Use “do..end” style multiline blocks for all blocks, even for one-line examples
* Create several test suites to speed up your workflow
RSpec Facts
Website: https://rspec.info/, https://github.com/rspec
License & Pricing: MIT License, open source / free
Support: Community
Documentation: https://rspec.info/documentation
More RSpec Knowledge
RSpec Best Practices
The Definitive RSpec Tutorial With Examples
Getting Started with RSpec
Ruby for Newbies: Testing with RSpec
My top 7 RSpec best practices
Getting Started With IronRuby And RSpec
Ruby for Newbies: Testing with Rspec
RSpec Videos
How to RSpec – Fairly comprehensive starter guide to RSpec
Acceptance Testing with RSpec and Capybara
Efficient Rails Test-Driven Development (1st of 6 episodes)
Your tests are lying to you