‘Subject’ RSpec Tests are K3WL

Today I wrote my first set of ‘subject’ tests for a pretty simple model. These tests are fast, clean, and easy to read. I wrote up a basic example to demonstrate and share some knowledge.

Super simple model:

class MailingAddress
 attr_accessor :name, :street, :city, :state, :zip

 def initialize(args)
  self.name = args[:name]
  self.street = args[:street]
  self.city = args[:city]
  self.state = args[:state]
  self.zip = args[:zip]

Now some super simple tests:

describe MailingAddress do
 context "when passed valid arguments" do
   subject do
     :name => "Matic",
     :street => "Test St.",
     :city => "Some City",
     :state => "CA",
     :zip => "90210",

  its(:name) { should == "Matic" }
  its(:street) { should == "Test St." }
  its(:city) { should == "Some City" }
  its(:state) { should == "CA" }
  its(:zip) { should == "90210" }

Uhhhhh…yes, please. I understand that these simple tests aren’t really testing anything too substancial, but it’s not like they are limited to the expected behavior of arguments. The possibilites of drying up legacy code are ENDLESS 😀

This entry was posted in Ruby. Bookmark the permalink.