Test your app with RSpec
Updated by Ana Schwendler, @anaschwendler
This guide assumes that you have already built a Rails Girls app by following the app development guide.
RSpec is a Ruby testing framework, that describes our application’s behavior in a syntax that doesn’t look much like Ruby. It outputs test results in your terminal, so you’ll test your reading skills as well (pun intended).
COACH: Talk about testing and Behavior-Driven Development.
1. Add RSpec gem
Open up your
Gemfile and add this line to the
:test groups, above the end tag:
to install the gem.
After that run the following command, in order to generate the folders for rspec:
This adds the following files which are used for configuration:
2. Create your first test!
Rubyists often use the words ‘test’ and ‘specification’ interchangeably, that’s why you’ll store your tests in the ‘specs’ folder. To do that, do the following steps:
We will be creating a test for our
idea model, to do that in the elegant way in Rails:
- Create a
modelsfolder in your
specfolder, by running in the terminal:
- Save your test as
Inside that new file, in our first test we will want to guarantee that an idea has a name. In order to do that let’s describe one of our specifications:
In your terminal run
which will output that your test is pending as it’s not yet implemented.
COACH: Talk about googling terminal output.
Let’s do something about that!
should give you a more satisfying output.
You could actually also create two ideas, to be sure that our project is creating ideas in the right way:
which test more things.
COACH: Talk a bit about refactoring.
4. Marking to-do’s with tests
Yeah! To-do lists. Awesome. A nifty RSpec feature is the functionality to mark certain tests as pending. In other words, your first thinking about what the implementation should accomplish then write in a test to verify if it is working.
Let’s create our next test, by adding the lines below to our
will mark a test as pending.
Can you finish this test? Can you think about other tests?
5. Behavior-Driven Development
COACH: Talk a bit about Behavior-Driven Development.
By now you can create more tests alone. Feel free to talk to your coach to do that, or ways to create more tests.
- Handy cheatsheet for Ruby, Rails, console etc.
- Guide 1: Guide to install Rails
- Guide 2: Build Your First App
- Guide 3: Push Your App to GitHub
- Guide 4: Put your app online with…
- Guide 5: Allow Comments on Your App
- Guide 6: Add design using HTML & CSS
- Guide 7: Create thumbnails with Carrierwave
- Guide 8: Add Authentication (user accounts) with Devise
- Guide 9: Add Profile Pics with Gravatar
- Guide 10: Improve your design with HTML and CSS
- Guide 11: Continuous Deployment
- Guide 12: Build a voting app in Sinatra
- Guide 13: Build a diary app in Ruby on Rails
- Guide 14: Add a back-end to your app (admin pages)
- Guide 15: Go through additional explanations for the App