Topic: [rSpec] Keeping my Specs DRY

Hey Guys

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
  before(:each) do
    @user = User.new
    @user.name = 'Test_User-12'
    @user.password = 'testpassword'
    @user.email = 'test@domain.com'
  end
 
  it 'should be valid' do
    @user.should be_valid
  end
 
  it 'should be invalid without a username' do
    @user.name = nil
    @user.should_not be_valid
  end
 
  it 'should be invalid if the username contains anything other than letters, numbers, hyphens and underscores' do
    @user.name = '+inv@lid username!'
    @user.should_not be_valid
  end
 
  it 'should be invalid if the email address is not in the correct format' do
    @user.email = 'broken-email.com'
    @user.should_not be_valid
  end
 
  it 'should be invalid if the password does not match the password_confirmation' do
    @user.password_confirmation = 'broken'
    @user.should_not be_valid
  end
end


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
end

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,

Andy

Re: [rSpec] Keeping my Specs DRY

There's an excellent article by Luke Redpath on BDD and rSpec, I recommend giving it a good read since he covers how to DRY up some behavior. RSpec has changed quite a bit since that article, but the basic principles still apply.

Railscasts - Free Ruby on Rails Screencasts

Re: [rSpec] Keeping my Specs DRY

Cheers Ryan, I read that article, the old syntax threw me off a bit but it was still really useful!

Re: [rSpec] Keeping my Specs DRY

There is another good point here, which is with RSpec and your specifications, the best idea is to keep the specs obvious, not clever.

In my opinion, perfect specs would be written in such a way that someone else could take the specs and reproduce your code from them.

Ie, all the "it shoulds" should define the behaviour so that you COULD take the specs and write the MCV's until you produced a passing spec test at which point the app would work.

So any DRYness you apply would be in improving readability and functionality, not so much to improve execution time of make the code look neat and DRY for DRYness sake.

This is in contrast to actual code which you want to refactor and make DRY per current rails best practices.

So, don't really worry about repeating yourself in specs as long as the result is an easy to read, obvious spec.

Regards

Mikel

Re: [rSpec] Keeping my Specs DRY

Well said Mikel, I agree.

Railscasts - Free Ruby on Rails Screencasts