CHIPS 8.5 (Part 2): Intro to RSpec on Rails
Apply TDD to a Rails app: test models and controllers in isolation using doubles, stubs, and seams. This is part of Module 8.
CHIPS 8.5 Part 2 is an individual assignment — you work on it and submit it on your own.
Goals
- Write model and controller specs for a Rails app with
rspec-rails. - Isolate the code under test using test doubles (mocks and stubs), including stubbing calls to external services (“stubbing the Internet”).
- Use Guard to re-run your specs automatically as you change code.
What you’ll do
Continuing from Part 1, you’ll use TDD
to add a “look up a movie in TMDb” feature to RottenPotatoes: the route, view,
and controller method first, then the model method that calls the external
TMDb service — which you’ll stub out with WebMock so your tests are Fast,
Independent, and Repeatable. You’ll also use Guard to re-run your specs
automatically as you code. The step-by-step instructions are in the
Online Instructions (also in the starter repo’s
instructions/
folder).
Use Getting the Code below to make your own private copy of the starter repo — you’ll be committing a TMDb API key to it, so it must not be public.
Before you start
Complete CHIPS 8.5 Part 1 and watch Module 8 §8.3–8.6.
Logistics
- Getting the code: see Getting the Code below — you clone the public starter repo and push it to a private repo of your own.
- Autograding: submit to Gradescope; you may submit as many times as you like before the deadline. The autograder is a way for you to check your work and your understanding. Run the tests locally first — see Running Code and Tests.
- Deployment: deploy your app the same way as in
CHIPS 5.3, and put its URL in
rottenpotatoes-url.txtbefore you submit. The autograder tests your deployed app and adds movies to its database — see the submission instructions for the details, including how to reset the database. - Extensions: everyone automatically gets a 2-day no-penalty extension; see the extension policy.
- Collaboration: this CHIPS is individual — you may discuss high-level approaches with anyone, but write and submit your own code. Sharing code is academic misconduct. See the AI Use Policy below before using AI tools.
AI Use Policy
This assignment follows the course AI Policy, which will be published on this site before it is needed. Until then, the rule from the Syllabus applies: you must be able to explain everything you submit.
Getting the Code
Public starter repo: saasbook/hw-rspec-rails-intro —
https://github.com/saasbook/hw-rspec-rails-intro
Checking Out Your Code
A GitHub fork of a public repo is always public, and your coursework has to stay private. So instead of clicking Fork, clone the starter repo and push it to a private repo of your own with
gh(see Local Development):git clone https://github.com/saasbook/hw-rspec-rails-intro.git cd hw-rspec-rails-intro git remote rename origin upstream gh repo create YOUR_GITHUB_USERNAME/hw-rspec-rails-intro --private --source=. --remote=origin --push git remote set-url --push upstream DISABLEDYou end up with two remotes:
originis your new private copy — push all of your work there — andupstreamissaasbook/hw-rspec-rails-intro, which you can pull our fixes from but, thanks to that last line, can never accidentally push to.
Keeping your private repo up to date
Your private repo is a snapshot: it does not follow the starter
repo. If we fix or add something in saasbook/hw-rspec-rails-intro
after you copy it, you have to pull those changes in yourself. The
upstream remote you set up above is how you do that:
git fetch upstream
git fetch only downloads — it changes nothing in your working
copy. Review what you are about to take before merging it:
git diff --name-only HEAD upstream/main # just the names of the files that differ
git diff --stat HEAD upstream/main # the same list, plus how much changed in each
git diff HEAD upstream/main -- spec # the actual changes, limited to one file or directoryThen merge the upstream changes into your branch and push them to your private repo:
git merge upstream/main
git push origin main
If you and we edited the same lines, git stops and reports a conflict: open
each file it lists, keep the correct combination of both sides, delete the
<<<<<<< / ======= /
>>>>>>> markers, then git add
those files and git commit. Ask on Ed if a merge leaves you
stuck — don't start over and lose your work.
These commands assume the starter repo's default branch is main.
If git branch -r shows upstream/master instead, use
that name in the commands above.
View this assignment on bCourses.
Running Code and Tests
Run everything below from the root of your clone (see Getting the Code). If Ruby isn't installed yet, work through Local Development first.
Install the dependencies
bundle install
Run this once after cloning, and again whenever the Gemfile
changes. Prefix the commands below with bundle exec so you run
the same gem versions the autograder does.
Boot the project
Create the database (once, and after any new migration), then start the Rails server:
bin/rails db:migrate
bin/rails serverThe app is then at http://localhost:3000; Control-C stops the server. To try one thing without clicking through the UI, use the console, which loads your app and its models:
bin/rails consoleRun the tests
Run the RSpec suite the way the Gradescope autograder will — get it green locally before you submit:
bundle exec rspec # the whole suite
bundle exec rspec spec/YOUR_FILE_spec.rb # one spec file
bundle exec rspec spec/YOUR_FILE_spec.rb:42 # one example, by line number
bundle exec rspec --format documentation # every example's descriptionEach failure prints the file and line of the failing example along with what it expected — start there rather than at the top of the output.