You are currently browsing the tag archive for the ‘Test’ tag.

Usage

@updated_at = lambda{ @model.updated_at.to_i }
assert_change(@updated_at) { @model.touch }
assert_no_change(@updated_at) { @model.reload }

Code

  def assert_change(what)
    old = what.call
    yield
    assert_not_equal old, what.call
  end

  def assert_no_change(what)
    old = what.call
    yield
    assert_equal old, what.call
  end

Make your rspec output look more interesting!
(yes we had too much spare time at our hands… :D )

# spec/spec_helper.rb
# encoding: UTF-8

if defined? RSpec::Core::Formatters::ProgressFormatter
  RSpec::Core::Formatters::ProgressFormatter.class_eval do
    DOTS = ['☘','⚘']
    def example_passed(example)
      output.print green(DOTS[rand(DOTS.size)])
    end
  end
end

So far our convention has been

  describe :method_name do 
    ...
  end

But this has some obvious drawback like separation of instance/class methods.
I hope we can now all agree on the unambiguous convention Mr Heinrich found in a great rspec presentation:

  • use “description” for non-methods
  • use pound “#method” for instance methods
  • use dot “.method” for class methods

This also makes prettier specdoc output :)

If you noticed spork startup getting slower when switching to Rails 2.3, your not alone ;)

  • Views are eager loaded
  • app/ is eager loaded (when config.cache_classes is on)

Without hack: 18s startup
With hack: 2s startup :D

Try it

#spec/spec_helper.rb
begin
  require 'spork/app_framework/rails'
  module Spork::AppFramework::Rails::NinjaPatcher
    # views are preloaded  spork must be restarted for view changes
    def delay_eager_view_loading
      puts "removed because i am too slow..."
    end

    # do not preload application files
    # alternatively urn off config.cache_classes
    def delay_app_preload
      ::Rails::Initializer.send(:define_method, :load_application_classes) do
      end
    end
  end
rescue
end

Spork.prefork do
  ...

If you got a big project, chances are spec_helper is required through different ways, File.expand_path / File.join() / … which results in it being loaded several times!

Try it:

#spec_helper.rb
print "YEP!"

rake spec -> “YEP!YEP!YEP!YEP!YEP!YEP!YEP!YEP!……”

So how to prevent it ?

Unify!
Unify it to ‘spec/spec_helper’ which is dead-simple, can be copied without modification (like adding ‘../../’) and prevents duplicate spec_helper calls

#unify_spec_helper.rb
require 'rubygems'
require 'rake'

files = FileList["spec/**/*_spec.rb"].reject{|file|File.directory?(file)}
files.each do |file|
  lines = File.readlines(file)
  lines = lines.map do |line|
    if line =~ /require.*spec_helper/
      "require 'spec/spec_helper'\n"
    else
      line
    end
  end
  File.open(file,'w'){|f| f.write lines.join('') }
end

Run: ruby unify_spec_helper.rb

Follow

Get every new post delivered to your Inbox.

Join 76 other followers