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 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.
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.
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.
We’d love to learn more about your project. Contact us below for a free consultation with our CEO.
Projects start at $50,000.