Test your app with RSpec
Originally created by Clemens Helm, @clemenshelm and Floor Drees, @floordrees. Updated by Ana Schwendler, @anaschwendler
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).
Talk about testing and Behavior-Driven Development.
1. Add the RSpec gem
Open up your Gemfile
and add this line to the :development
and :test
groups, above the end tag:
and run the following command to install the Ruby gem.
Then run the following command, in order to setup RSpec in your app:
This adds the following files which are used for configuration:
.rspec
spec/spec_helper.rb
spec/rails_helper.rb
2. Create your first test!
Rubyists often use the words ‘test’ and ‘specification’ interchangeably, that’s why you’ll store your tests in the ‘spec’ directory. To do that, do the following steps:
We will be creating a test for our Idea
model. Create a models
directory in your spec
directory. Then create a new file called idea_spec.rb
(<model_name>_spec.rb
) in that directory.
Inside the new test file, we will want to make sure our idea has a name. In order to do that let’s describe one of our specifications:
In the Terminal app run:
which will output that your test is “pending” as it’s not yet implemented.
Let’s add something to our test to make sure the idea has a name!
Run RSpec again, and you should see more satisfying green output this time, meaning all tests passed.
3. Make to-do’s with tests
Yeah! Let’s create to-do lists. Awesome!
A nifty RSpec feature is the functionality to mark certain tests as pending like we saw before. In other words, first you think about what your models should do, before writing the implementation and the test. You have an idea of what your model should do beforehand, and then you can write the code and tests for it.
Let’s create our next test, by adding the lines below to our idea_spec.rb
it will add another test and mark it as pending.
Can you finish this test? Can you think about other tests?
Consult the RSpec documentation, or search for an RSpec tutorial online, to find out what kind of tests you can make.
4. Add more tests
We have our first test, we test if our idea has a name, but our idea can hold many more things. Let’s test if it has comments or not.
5. Behavior-Driven Development
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.
Happy testing!
If you’re ever stuck during a guide, please ask your coach for help and also consult this handy cheatsheet for Ruby, Rails, the console, the Text Editor etc.
Guides
- Guide 1: Start of the guide
- Guide 2: Get to know the tools
- Guide 3: Guide to install Rails
- Guide 4: Build Your First App
- Guide 5: Style your app using HTML and CSS
- Guide 6: Add a new page to your app
- Guide 7: Add a new homepage to your app
- Guide 8: Add picture uploads
- Guide 9: Push Your App to GitHub
- Guide 10: Put your app online with one of these services:
- Guide 11: Style the idea pages using HTML and CSS
- Guide 12: Add comments to your app
- Guide 13: Create picture thumbnails
- Guide 14: Test your app with RSpec (Current page!)