Skip to content

Instantly share code, notes, and snippets.

@bmabey
Forked from diabolo/customer_spec.rb
Created June 18, 2009 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmabey/131701 to your computer and use it in GitHub Desktop.
Save bmabey/131701 to your computer and use it in GitHub Desktop.
macro example and variations
describe Customer, "last billing address" do
before do
@customer = Customer.generate!
@customer = Address.new
end
it "should be settable and gettable" do
@customer.last_billing_address = @account_address
@customer.last_billing_address.should == @account_address
end
...
end
describe Customer, "last delivery address" do
before do
@customer = Customer.generate!
@account_address = Address.new
end
it "should be settable and gettable" do
@customer.last_delivery_address = @account_address
@customer.last_delivery_address.should == @account_address
end
end
share_examples_for "named address" do
it "should be settable and gettable" do
named_address = @account_address
@named_address.should == @account_address
end
end
describe Customer, "last billing address" do
it_should_behave_like "named address"
before do
named_address = @customer.last_billing_address=
@named_address = @customer.last_billing_address
class Customer
attr_accessor :last_delivery_address, :last_billing_address
end
module AccessorMacro
def it_should_have_an_accessor_for(*attributes)
attributes.each do |attribute|
describe attribute do
it "should be settable and gettable" do
reader = attribute.gsub(' ','_')
writer = "#{reader}="
@customer.send(writer,'some value')
@customer.send(reader).should == 'some value'
end
end
end
end
end
describe Customer do
before do
@customer = Customer.new
end
extend AccessorMacro
it_should_have_an_accessor_for('last billing address', 'last delivery address')
end
class Customer
attr_accessor :last_delivery_address, :last_billing_address
end
module AccessorMacro
def it_should_have_an_accessor_for(*attributes)
attributes.each do |attribute|
it "has an accesor for #{attribute}" do
reader = attribute.to_s.gsub(' ','_')
writer = "#{reader}="
@customer.send(writer,'some value')
@customer.send(reader).should == 'some value'
end
end
end
alias :it_has_accessors_for :it_should_have_an_accessor_for
end
describe Customer do
before do
@customer = Customer.new
end
extend AccessorMacro
it_has_accessors_for :last_billing_address, :last_delivery_address
end
class Customer
attr_accessor :last_delivery_address, :last_billing_address
end
module AccessorMacro
def it_should_have_an_accessor_for(*attributes)
attributes.each do |attribute|
it "has an accesor for #{attribute}" do
reader = attribute.to_s.gsub(' ','_')
writer = "#{reader}="
described_object = instance_variable_get("@#{self.class.described_type.to_s.downcase}") || self.class.described_type.new
described_object.send(writer,'some value')
described_object.send(reader).should == 'some value'
end
end
end
alias :it_has_accessors_for :it_should_have_an_accessor_for
end
describe Customer do
before do
@customer = Customer.new
end
extend AccessorMacro
it_has_accessors_for :last_billing_address, :last_delivery_address
end
class Customer
attr_accessor :last_delivery_address, :last_billing_address
end
module AccessorMacro
def it_should_have_an_accessor_for(*attributes)
attributes.each do |attribute|
it "has an accesor for #{attribute}" do
reader = attribute.to_s.gsub(' ','_')
writer = "#{reader}="
subject.send(writer,'some value')
subject.send(reader).should == 'some value'
end
end
end
alias :it_has_accessors_for :it_should_have_an_accessor_for
end
describe Customer do
# This isn't really needed, but it shows how you would define a subject with your current setup.
#before do
#@customer = Customer.new
#end
#subject { @customer }
extend AccessorMacro
it_has_accessors_for :last_billing_address, :last_delivery_address
end
class Customer
attr_accessor :last_delivery_address, :last_billing_address
end
module AccessorMacro
def it_should_have_an_accessor_for(*attributes)
attributes.each do |attribute|
class_eval(<<-END_OF_MACRO, __FILE__, __LINE__ + 1)
it "has an accesor for #{attribute}" do
subject.#{attribute}= 'some value'
subject.#{attribute}.should == 'some value'
end
END_OF_MACRO
end
end
alias :it_has_accessors_for :it_should_have_an_accessor_for
end
describe Customer do
extend AccessorMacro
it_has_accessors_for :last_billing_address, :last_delivery_address
end
class Customer
attr_accessor :last_delivery_address, :last_billing_address
end
module AccessorMacro
def it_should_have_an_accessor_for(*attributes)
source_filename, source_line = caller[0].split
attributes.each do |attribute|
class_eval(<<-END_OF_MACRO, source_filename, source_line.to_i)
it "has an accesor for #{attribute}" do
subject.#{attribute}= 'some value'
subject.#{attribute}.should == 'some value'
end
END_OF_MACRO
end
end
alias :it_has_accessors_for :it_should_have_an_accessor_for
end
describe Customer do
extend AccessorMacro
it_has_accessors_for :last_billing_address, :last_delivery_address
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment