Set up RSpec for Ruby applications
For all Ruby applications you can use the rspec gem. The gem is a collection of 3 other gems:
You can also include one or more of these gems separately. For instance, if you want to use Bogus for mocking, like we did in last week’s episode, you can just add rspec-coreand rspec-expectations to your Gemfile, because you don’t need rspec-mocks.
If you run
rspec from your project’s root directory without any argument, it will expect your specs to be in the
./spec directory and your classes to be in the
./lib directory. Although you can work around this assumption, I recommend you to stick to them: They are applied in almost all Ruby projects.
Set up RSpec for Rails applications
These rspec gem will work for every Ruby application you create, so you could also use it with your Rails applications. However, there is a more convenient option: The rspec-railsgem is the easiest way of setting up RSpec for Rails. It will provide you with convenient generators that get called when you generate resources like models or controllers. Watch the screencast to see it in action!
Up next: Setting up Cucumber
Next week we’ll set up our Ruby and Rails applications with Cucumber. In the meantime check out our other episodes on RSpec. You can find a list of them below in the “Further info” section.