Notes posted by henning
RSS feedHow to test callback methods
When testing callback methods, try to test the callback chain separate from its implementation.
Say this is your model:
class Project belongs_to :owner has_many :milestones after_save :create_milestones after_save :notify_owner private def notify_owner owner.project_created! end def create_milestones milestones.create(:name => 'Milestone 1') end end
You should write your spec like this:
describe Project do describe 'create_milestones' do it 'should create an initial milestone' do project = Project.new project.milestones.should_receive(:create) project.send(:create_milestones) end end describe 'notify_owner' do it 'should notify its owner' do project = Project.new(:owner => mock_model(User)) project.owner.should_receive(:project_created!) project.send(:notify_owner) end end describe 'after_save' do it 'should run the proper callbacks' do project = Project.new project.should_receive(:create_milestones) project.should_receive(:notify_owner) project.run_callbacks(:after_save) end end end
Here is some more advice on how to test callback methods in Rails:
http://gem-session.com/2010/03/how-to-test-callback-methods-in-rails
Using models in your migration
Here is some advice how to call your models in a migration without shooting yourself in the foot:
http://gem-session.com/2010/03/how-to-use-models-in-your-migrations-without-killing-kittens
Basically you can inline models into your migrations to decouple them from changes in your model:
class AddCurrentToVendor < ActiveRecord::Migration class Vendor < ActiveRecord::Base end class Article < ActiveRecord::Base has_many :vendors, :class_name => 'AddCurrentToVendor::Vendor', :order => 'created_at' end def self.up add_column :vendors, :current, :boolean Article.all.each do |article| article.vendors.first.andand.update_attribute(:current, true) end end def self.down remove_column :vendors, :current end end
How to test custom error pages
Here is some advice for testing custom error pages using Webrat and Cucumber:
http://gem-session.com/2010/03/testing-your-custom-error-pages-with-webrat-and-cucumber