What makes a good RSpec test?
RSpec is a testing framework written in Ruby to test Ruby code. To get started using RSpec with Rails, add it to the Gemfile.
Next finish setting it up by running bundle install in your project directory and then
It is easy to test for expected results, but how do you test for the unexpected? Good tests should include all edge cases, including best and worst-case scenarios; they should also be streamlined, very readable, and have redundancies eliminated.
Readability
Readability is as much about containment as it is eliminating obfuscation, intentional or otherwise.
Use contexts with scenarios to isolate possible edge cases.
Keep the waters from getting muddied when testing different methods; prepend `.` when testing class and `#` when testing instance.
Ideally, you’ll keep each test as distinct and separate as possible so running down each one takes less work and makes for a more efficient process.
Don’t Repeat Yourself: DRY up your tests
When you find yourself writing the same setup for multiple tests, put it in a shared context. Again, efficiency is the name of the game. Keeping tests as focused as possible will save time and effort.
Shared examples are beneficial when testing the behavior of different types.
Speed up your Tests
Long test running times can be painful. If you’re keeping your tests separated and eliminating redundancies, you’ll want to zero in on any taking longer than they should.
To see your top 10 slowest tests, append --profile to your rspec command.
In controller tests, you can reduce network requests by using before blocks and modifying the parameters in each context.
Resources to learn more about using RSpec with Ruby
https://github.com/rspec/rspec-rails
https://pragprog.com/book/rspec3/effective-testing-with-rspec-3
Conclusion
Using techniques like what is listed above, we have had the opportunity to address clients’ concerns and they love it! If you are interested in joining our team, please visit our Careers page.