The first, and the “good enough” reason to test your Ruby on Rails application thoroughly is that Ruby is not a compiler but an interpreter, if not tested your application can halt even on a banal syntax error.
Introduction
If you are:
- working alone on a project, and/or
- having confidence in yourself being a perfect coder without making any bugs, and/or
- too busy to write test code, and/or
- ….
you might use RSpec for documentation purposes only. In minutes you can generate a requirement specification document like:
User relations : - has one or more Roles - has one or more Orgcharts - belongs to one or more Orgcharts - has one or more Executions - has one or more Tasks through Participants - on deleting User also the Participants will be removed User includes - shares code with DatabaseHelper User attributes security - attributes accessible: User validations - :login must be present - :email must be present - :password must be present - :password_confirmation must be present - :login length must be - :email length must be - :password length must be - :login must be unique - :email must be unique - format of :login must be - format of :email must be - format of :password must be User is managed by ActiveScaffold - must get the Validation Reflections - maps :login to :name User Acts As Authenticated - there are predefined roles - autheticates a user based on username and passowrd - encrypts data with SHA1 - encrypts password with SALT - checks if authenticated? ??? - remember token??? - remembers the user in the cookie - forgets the remembered user - encrypts password - checks if password is required
DRY / Spec reuse
If you’ve got the taste of having a good doc in no time, and still you think this is:
- boring
- not DRY
- time consuming writing the same specs for all your models,
consider RSpec helpers.
- Create your own helper file in “/spec” root, like “spec/spec_shared_model.rb”;
- Copy here the reusable specs and add “:shared => true”
- Include the helper in your other spec files (user_spec.rb)
- Use “it_should_behave_like” in user_spec.rb
describe "ActiveScaffold", :shared => true do it "must get the Validation Reflections" do end it "maps :login to :name" do end end
require File.dirname(__FILE__) + '/../spec_helper' require File.dirname(__FILE__) + '/../spec_shared_model'
describe "is managed by ActiveScaffold" do it_should_behave_like "ActiveScaffold" end


