Hey there! We're thoughtbot, a design and development consultancy that brings your digital product ideas to life. We also love to share what we learn.
This coding exercise comes from Upcase, the online learning platform we run. It's part of the Rails Testing Exercises course and is just one small sample of all the great material available on Upcase, so be sure to visit and check out the rest.
Testing an ActiveRecord model is similar to testing a simple Ruby object, but you'll need to go through the public ActiveRecord API to test your functionality. For example, you'll need to run save
or valid?
to generate error messages from your validations.
In this exercise, you'll write an RSpec model test to verify a simple validation.
To start, you'll want to clone and run the setup script for the repo
git clone [email protected]:thoughtbot-upcase-exercises/testing-fundamentals-testing-activerecord.git
cd testing-fundamentals-testing-activerecord
bin/setup
- Edit
spec/models/person_spec.rb
. - Write a spec to ensure that the model is invalid without a
first_name
. - Make sure your specs are passing by running
rake.
Check out the featured solution branch to see the approach we recommend for this exercise.
If you find yourself stuck, be sure to check out the associated Upcase Forum discussion for this exercise to see what other folks have said.
When you've finished the exercise, head on back to the Rails Testing Exercises course to find the next exercise, or explore any of the other great content on Upcase.
testing-fundamentals-testing-activerecord is Copyright © 2015-2022 thoughtbot, inc. It is free software, and may be redistributed under the terms specified in the LICENSE file.
This exercise is maintained and funded by thoughtbot, inc.
The names and logos for Upcase and thoughtbot are registered trademarks of thoughtbot, inc.