Topic: [rSpec] Keeping my Specs DRY
I finally got round to starting my first rails app. Hooray!
About half way through creating my first model, and starting to add more complex behavior and slowly losing track of what on earth everything was doing I realised I needed to think about testing. I had already decided that I liked the idea of BDD and got started with rSpec.
I have so far written 5 examples which look a little something like:
require File.dirname(__FILE__) + '/../spec_helper'
describe User, ' in General' do
@user = User.new
@user.name = 'Test_User-12'
@user.password = 'testpassword'
@user.email = 'email@example.com'
it 'should be valid' do
it 'should be invalid without a username' do
@user.name = nil
it 'should be invalid if the username contains anything other than letters, numbers, hyphens and underscores' do
@user.name = '+inv@lid username!'
it 'should be invalid if the email address is not in the correct format' do
@user.email = 'broken-email.com'
it 'should be invalid if the password does not match the password_confirmation' do
@user.password_confirmation = 'broken'
The first question is, am I going about this BDD thing correctly?
The next thing, I was going to create another description, this time:
describe User, ' which has not been activated' do
Obviously, I will be testing the same data, just a different aspect. So do I need to repeat the @user set up of is there a way I can extract this and make it much more DRY?
Thanks for any help in advance,