The create_bank method looks like this:
def create_bank( options = {})
Bank.create({
:person_id => 1,
:sort_code => ‘989898’,
:name => ‘Natlays Shanghai’,
:branch => ‘Fruit Exchange’,
:address => ‘Big Street’,
:tow => ‘Tow’,
:county => ‘Place’,
:postcode => ‘L1 5XX’,
:account => ‘123456789’,
}.merge(options))
end
RSpec tests look a bit like this
it “should be valid” do
lambda {
create_bank.should be_valid
}.should change(Bank,:count).by(1)
end
it “should have a sort code” do
lambda {
bank = create_bank(:sort_code => nil)
bank.should have_at_least(1).errors_on(:sort_code)
}.should_not change(Bank,:count)
end
it “should belong to a perso” do
Bank.reflect_on_association(:person).should_not be_nil
end
Testing that destroy is cascaded down to child records (Employer can have many addresses):
it “should delete addresses” do
address = Address.new
employer = create_employer
employer.addresses « address
address.should_receive(:destroy).once
employer.destroy
end