SpicyCode

The ramblings of Chad Humphries

Posts Tagged ‘rspec

RSpec 1.0

RailsConf was quite fun this and I finally got a chance to meet David Chelimsky and Aslak Hellesøy on Saturday. Ken and I sat down with them and coded a patch or two for RSpec 1.0.

We got the “not implemented” spec feature added during this time. It worked like this:

describe YourClass do

  it "should do something awesome later"

end

By not giving the example (‘it’) a block you get a Not Implemented spec notice in all the various outputs. This will be yellow in the console output when color is on, and the same in the html output. Having not implemented specs will not break your build, and they do not count as errors or failures.

All in all a very fun time.

Advertisements

Written by spicycode

May 22, 2007 at 6:10 pm

Posted in programming

Tagged with , ,

RSpec Extensions: Now in plugin form

After releasing the first version of the spicycode rspec extensions as a gem I’ve determined my development style (mainly controlled by free time) is currently much more suited to plugins.

So with that revelation at hand I’m here to announce the latest version of my RSpec Extensions featuring:

  • Several bug fixes, and expanded usage versions of existing validation matchers
  • New expectation matchers added in for basic be_valid, uniqueness, and work on the generic base validation expectation matcher.
  • Working association validation matchers

Install if you dare from the following subversion repository: http://svn.theedgecase.com/edgecase/goodies/plugins/rspec_expectation_matchers/

Many of the new extensions are from various blogs about the internet, and are clearly attributed as such. The goal of this project is to provide a complete suite of expectation matchers for common activerecord behaviour and operations.

Written by spicycode

April 22, 2007 at 5:51 pm

Posted in programming

Tagged with ,

Rspec Expectation Matchers – Part II

The new release of my RSpec extensions gem (along with a newly minted plugin version) will be out within a day or two. Barring any major show-stoppers I just need to finish adding :message => specification support so you can indicate what error message you expect to receive if it is invalid (if you choose to). Once that is in I’ll let the release notifications fly.

The goal of these expectation matchers is to allow you to test validations, and associations easily. Whether or not you wish to test these items I will leave up to you, but if you choose to this will definitely help keep things simple and quick.

Poor mans code outline

validate_presence_of(attr, options = { })
#    ex:  model.should validate_presence_of(:name)
#           model.should validate_presence_of(:name, :valid_value => 'Bob', :invalid_value => nil)
validate_confirmation_of(attr, options = { })

validate_uniqueness_of(attr, options = { })

validate_acceptance_of(attr, options = { })

validate_associated(associated_model, options = { })

validate_exclusion_of(attr, options = { })

validate_format_of(attr, options = { })

validate_inclusion_of(attr, options = { })

validate_length_of(attr, options = { })
alias_method :validate_size_of, :validate_length_of
#    ex:  model.should validate_length_of(:name, :within => 3..30)
#          model.should validate_length_of(:name, :maximum => 100)

validate_numericality_of(attr, options = { })

have_association(type, name, options = {})

belong_to(name, options = {})
#  ex: model.should belong_to(:parent)
#             (or more detail) model.should belong_to(:parent, :class_name => 'People')
have_one(name, options = {})

have_many(name, options = {})

have_and_belong_to_many(name, options = {})

Example usage:

context "A User" do

  setup do
    @user = User.new(valid_user_attributes)
  end

  specify do
    @user.should validate_presence_of(:first_name)
  end

  specify do
    @user.should validate_length_of(:first_name, :in => 1..50)
  end

  specify do
    @user.should validate_presence_of(:last_name)
  end

  specify do
    @user.should validate_length_of(:last_name, :in => 1..50)
  end

  specify do
    @user.should validate_presence_of(:username)
  end

  specify do
    @user.should validate_length_of(:username, :in => 3..40)
  end

  specify do
    @user.should validate_presence_of(:email)
  end

  specify do
    @user.should validate_length_of(:email, :in => 3..100)
  end

  specify do
    @user.should validate_presence_of(:password)
  end

  specify do
    @user.should validate_confirmation_of(:password)
  end

  specify do
    @user.should validate_presence_of(:account_id)
  end

  specify do 
    @user.should belong_to(:account)
  end

  specify do 
    @user.should have_many(:project_assignments)
  end  

  specify do 
    @user.should have_many(:projects, :source => :project, :through => :project_assignments, :order=>"projects.name")
  end

end

Example output:

A User
- should validate presence of first_name
- should validate length of first_name
- should validate presence of last_name
- should validate length of last_name
- should validate presence of username
- should validate length of username
- should validate presence of email
- should validate length of email
- should validate presence of password
- should validate confirmation of password
- should validate presence of account_id
- should have a belongs_to association called :account
- should have a has_many association called :project_assignments
- should have a has_many association called :projects

Written by spicycode

April 1, 2007 at 5:55 pm

Posted in programming

Tagged with , ,

oooh shiny! rspec extensions for activerecord validations

I’m currently in waiting for rubyforge approval limbo for my new rspec extensions project but I figured I would post about it anyway.

Step 1 – Identify something that annoys you

  • In this case repeatedly writing validation specifications

Step 2 – Fix a problem

  • Write gem to package up solution…in this case spicycode_rspec_extensions

Step 3 – Profit?

  • Ok, so no step three, but it sounded good at the time.

So what does it do?

I started out with the simplest cases I was repeating basing it off of David’s excellent post about custom expectation matchers, more will come later. See the awe-inspiring and mind boggling (well, at least mind boggling) examples below. This does require rspec 0.8.2 or above.


context 'A User (when validating)' do
  ...
  setup do
    @user = User.new(valid_user_attributes)
  end

  specify do
    @user.should require_presence_of :first_name
  end

  specify do
    @user.should require_confirmation_of :password
  end
   ...
end

After running you will get output like the following thanks to RSpec’s automatic naming abilities


A User (when validating)
- should validate presence of first_name
- should validate confirmation of password

Finally, if something were to not match it will print out what was wrong with the object (the objects current errors).

Recap?

Well hopefully some of what I said made sense to someone 🙂 I’ll be pushing the gem up to ye olde ‘forge as soon as the project approval comes through.

Written by spicycode

March 19, 2007 at 5:56 pm

Posted in programming

Tagged with , ,

SpicyCode Rspec Extensions are a go for launch

We are go for launch, the ‘forge has approved us. You should see my rspec_extensions gem up today on RubyForge.

Written by spicycode

March 19, 2007 at 5:55 pm

Posted in programming

Tagged with ,

Custom expectations in rspec (pre 0.8.0)

Nothing special, just a simple helper I threw together a while back. It is used in model tests to verify that a column is required. A few usages follow. Note: this will be completely replaced in rspec 0.8.0 by custom expectation matchers. But I’ve had this one around for a while in one form or another, so I figured I’d post it up in case it helps anyone.

The code

 1   def should_require_attribute(model, attrib_name, error_messages=[], invalidvalue=nil)
 2     # Collect the valid value
 3     valid_value = model.send(attrib_name)
 4     error_messages = error_messages.to_a.sort
 5     
 6     # Set it to nil
 7     model.send("#{attrib_name.to_s}=", invalid_value)
 8 
 9     model.should_not_be_valid
10     model.should_have(error_messages.size).errors
11     model.errors.on(attrib_name).to_a.sort.should == error_messages
12     
13     # Put things back where you found them
14     model.send("#{attrib_name.to_s}=", valid_value)
15     model.should_be_valid
16   end

Usage

 1 # Simple case
 2 specify "should be invalid without state" do
 3    should_require_attribute(your_model, :state, "is required")   
 4 end
 5 
 6 # More than one error expected
 7 specify "should require a valid email address" do
 8    should_require_attribute(your_model, :email_address, ["is required", "should be a valid format"])   
 9 end
10 
11 # Invalid value other than nil
12 specify "should require a non .gov email address" do
13    should_require_attribute(your_model, :email_address, "can't contain .gov domains", "bigbrother@government.gov")   
14 end

Written by spicycode

February 23, 2007 at 6:00 pm

Posted in programming

Tagged with , ,

RSpec 0.7.4

Not much to say other than fire up your terminal of choice and type:

sudo gem update

Get some goodness with the new RSpec 0.7.4. This release polishes many of the new in-transit features of 0.7.x and adds Luke RedPath’s awesome new html report.

You can read the full changelog here.

Written by spicycode

December 3, 2006 at 6:33 pm

Posted in programming

Tagged with ,