Enhanced RSpec Profiling

If normal rspec profile output is not enought, try this enhanced rspec profile formatter!

Example

Groups:
5.1150300 Movie
4.9603220 Icon
1.7279670 User
1.4466160 Person
...

Single examples:
4.8707710 Icon refresh! resizes existing thumbs
0.9086340 Review releases the movie
0.5203390 Movie finds invalid
...

Install

script/plugin install script/plugin install git://github.com/grosser/rspec_enhanced_profile.git

#add to spec/spec.opts:
--format RspecEnhancedProfile:tmp/profile.txt

Profiling RSpec

Small option, but took me 2 hours to find :/
This will automatically generate reports for your test-runs.

It only shows single examples, which is sad since i would also want to know which controller_specs take the longest…

Output:

Top 10 slowest examples:
0.5215740 User downgrades to a person
0.4326950 User finds invalid
0.1914630 User remembering unsets remember token
0.1218360 User is valid
0.0903790 Festival is valid

Install:

#spec/spec.opts
--format profile:spec/profile.txt

Readable Specs: it renders, it assigns, it flashes

Today i got struck by simplicity, as a reviewer said:
what exactly is response.should render_template(“new”)
and my answer was: it says that it renders new

Usage

  #delete this
  flash[:error].should_not be_nil
  response.should render_template("new")
  response.should redirect_to('/')
  response.should redirect_to(user_path(User.first))

  #enjoy this
  it_flashes :error
  it_renders :new
  it_redirects '/'
  it_redirects User.first

Install

#spec/spec_helper.rb
def it_redirects_to(what)
  what = send(what.class.to_s.underscore+'_path',what) if what.kind_of? ActiveRecord::Base
  response.should redirect_to(what.to_s)
end
alias :it_redirects :it_redirects_to

def it_renders(what)
  response.should render_template(what.to_s)
end

def it_has_flash(what)
  flash[what].should_not be_blank
end
alias :it_flashes :it_has_flash
  
def it_assigns(what,to=nil)
  if to
    assigns[what].should == to
  else
    assigns[what].should_not be_blank
  end
end

Shouldless convention as outlined in my groundbreaking paper. 😉

Record Gettext at Test Runtime

Recently our team got frustrated with all the phrases gettext would not find.

  • words in if blocks
  • word produced by helpers
  • words used in arrays that get translated at runtime

Here is a simple solution: collect all phrases that were used during testing. Since the testsuite often has  100% C0 coverage and any phrase that is not found will signals missing tests.

Install

#spec/spec_helper.rb or test/test_helper.rb
if ENV['LOG_GETTEXT']
  def _(word)
    File.open(ENV['LOG_GETTEXT'], File::WRONLY|File::APPEND|File::CREAT) do |f|
      f.puts word
    end
    gettext(word)
  end
end


#lib/taskts/gettext_test_log.rb
task :gettext_test_log do
  tmpfile = 'locale/tmp_gettext_test_log.txt'
  outfile = "app/testlog_phrases.rb"
  
#  system "rake test LOG_GETTEXT=#{tmpfile}"
  system "rake spec LOG_GETTEXT=#{tmpfile}"
  process_log(tmpfile,outfile)
end

def process_log(tmpfile,outfile)
  found = {}
  File.readlines(tmpfile).each do |line|
    line.strip!
    next if line.empty?
    next if line =~ /%s of /
    next if %w[nil Sun Mon Tue Wed Thu Fri Sat Sunday Monday Tuesday Wednesday Thursday  Friday  Saturday  Jan Feb Mar  Apr Jun  Jul Aug Sep Oct Nov Dec January February March April May June July August September October November December].include? line
    found[line]=true
  end
  
  File.open(outfile,'w') do |f|
    found.each do |k,v|
      f.puts "_('#{k}')"
    end
  end
end

Usage
The generated testlog_phrases.rb should be placed in a folder that is search by your updatepo task(it is not meant to be executed).