Chris Sund
2009-Jul-23 21:26 UTC
[rspec-users] What else should I include in this model spec?
Hey everyone,
I just created a spec for an existing model I already developed prior
to reading the rspec book. I have 5 examples and they are all passing.
I''m just not sure what else I need to add or how I implement it. In
this model I have some methods for adding existing names to an
account, and also geocoding addresses for my google map. Here''s my
model code....followed by my current spec file. In my spec file I
basically added my validation information, I''m just not sure how to
add the other stuff. Any suggestions are appreciated. Keep in mind
I''m trying to get my current app up to date with testing procedures. I
wrote a portion of the app prior to having any knowledge of BDD and
now I''m playing catch up on the testing side of things. In every case
I''m also getting familiar with the ruby syntax - if you see something
seriously wrong let me know.
Many Thanks! - this group is great.
MODEL ACCOUNT.RB------------
class Account < ActiveRecord::Base
has_many :names, :dependent => :destroy
has_many :readings, :dependent => :destroy
validates_presence_of :s_property_no, :s_service_address, :s_service_address_zip
# On the fly Geocoding
acts_as_mappable
before_validation_on_create :geocode_address
before_validation_on_update :geocode_address
# Thinking Sphinx Search Indexes
define_index do
indexes s_property_no, :sortable => true
indexes s_service_address, :facet => true
indexes names.name, :as => :property_name
end
# adding human attributes to error messages
HUMAN_ATTRIBUTES = {
:s_property_no => "Property Number"
}
def self.human_attribute_name(attr)
HUMAN_ATTRIBUTES[attr.to_sym] || super
end
# end human attributes
def new_name_attributes=(name_attributes)
name_attributes.each do |attributes|
names.build(attributes)
end
end
after_update :save_names
def existing_name_attributes=(name_attributes)
names.reject(&:new_record?).each do |name|
attributes = name_attributes[name.id.to_s]
if attributes
name.attributes = attributes
else
names.delete(name)
end
end
end
def save_names
names.each do |name|
name.save(false)
end
end
# Map Information for Geocoding
def full_address
"#{s_service_address},#{s_service_address_zip}"
end
private
def geocode_address
if s_service_address_changed? or s_service_address_zip_changed?
geo = GeoKit::Geocoders::MultiGeocoder.geocode(full_address)
errors.add(:address, "Could not geocode address" ) unless geo.success
self.s_latitude, self.s_longitude = geo.lat, geo.lng if geo.success
end
end
end
CURRENT SPEC FILE - ACCOUNT_SPEC.RB ----------------------------
require File.expand_path(File.dirname(__FILE__) +
''/../spec_helper'' )
describe Account do
describe "validations" do
before(:each) do
@account= Account.new(:s_property_no
=>"1000", :s_service_address => "1201 Washington
St", :s_service_address_zip => "59601" )
end
it "should be valid with valid attributes (property no, service
address and zip)" do
@account.should be_valid
end
it "should not be valid without a service address or zipcode" do
@account.s_service_address = nil
@account.s_service_address_zip = nil
@account.s_property_no = nil
@account.should_not be_valid
end
it "should have one error on service address" do
@account.s_service_address = nil
@account.should have(1).error_on(:s_service_address)
end
it "should have one error on property number" do
@account.s_property_no = nil
@account.should have(1).error_on(:s_property_no)
end
it "should have one error on service address zip" do
@account.s_service_address_zip = nil
@account.should have(1).error_on(:s_service_address_zip)
end
end
end