RSpec response.should Information Enhancers

In case of failure the normal should be_redirect and its friends are far from helpful…

expected redirect? to return true, got false

expected "new", got nil

Now you can have this:

Status should be redirect but was 200(success)
 - rendered addresses/new
 - Flash:
    :error = Address contains errors!
 - Errors:Errors on @address(Address):
     City can't be blank

Usage

  • response.should redirect_to / render_template as normal
  • response.should have_been_success (i do not want to overwrite be_success…)
  • have_been_error
  • have_been_missing
  • have_been_redirect

Install

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

add to spec/spec_helper.rb:
Spec::Runner.configure do |config|
  ...
  config.include(RspecResponseEnhancer)
  ...
end

And as added benefit you no longer need to build should be_success when you already have a should render_template, since render_template now gives detailed error messages!

Autotest RSpec Notifications for Ubuntu

Get a popup every time autotest runs your tests with the current result.


Install:

sudo gem install ZenTest
sudo apt-get install libnotify-bin

.autotest

require 'autotest/redgreen'

module Autotest::Notify
  def self.notify title, msg, img, pri='low', time=3000
    `notify-send -i #{img} -u #{pri} -t #{time} '#{msg}'`
  end

  Autotest.add_hook :ran_command do |autotest|
    results = [autotest.results].flatten.join("\n")
    output = results.slice(/(\d+)\s+examples?,\s*(\d+)\s+failures?(,\s*(\d+)\s+pending)?/)
    folder = "~/Pictures/rails/"
    if output  =~ /[1-9]\d*\sfailures?/
      notify "FAIL:", "#{output}", folder+"rails_fail.png", 'critical', 10000
    elsif output  =~ /[1-9]\d*\spending?/
      notify "PENDING:", "#{output}", folder+"rails_pending.png", 'normal', 10000
    else
      notify "PASS:", "#{output}", folder+"rails_ok.png"
    end
  end
end
"

The icons go into your ~/Pictures/rails folder

Alternatively, one could use build-in pictures “gtk-dialog-error” / “gtk-dialog-info”…

Fighting the Spec Bloat

Every time i run rspec_scaffold and look at the generated specs im shocked anew, there is just so much bla bla and so little information. I know that as a rule of thumb there should be one assertion per test, but in my opinion this is going too far. Note to self: New rule of thumb: “Test one aspect per test”. Meaning that i test ‘database interaction’ and ‘output’ (renders x + assigns y + flash z) in different tests. And when there is only simple logic (1-liner) in either of them, they may be merged.

OLD

before(:each) do
  @address = mock_model(Address)
  Address.stub!(:find).and_return([@address])
end

def do_get
  get :index
end

it "should be successful" do
  do_get
  response.should be_success
end

it "should render index template" do
  do_get
  response.should render_template('index')
end

it "should find all addresses" do
  Address.should_receive(:find).with(:all).and_return([@address])
  do_get
end

it "should assign the found addresses for the view" do
  do_get
  assigns[:addresses].should == [@address]
end

NEW

it "should render index and assign all addresses" do
  Address.expects(:find).with(:all).once.returns([@address])
  
  get :index
  
  response.should render_template('index')
  assigns[:addresses].should equal([@address])
end

Colorful RSpec Stories

I do not know why there are no colors in the stories, but here is a small monkeypatch to fix this… (setting option[color] and not overwriting them would be better, but i could not find where to set them…)

#vendor/plugins/rspec/lib/spec/runner/formatter/story/plain_text_formatter.rb line ~50
#COLORIZE!
@options.colour = true#HACK
out = "#@count scenarios: #@successful_scenario_count succeeded, #{@failed_scenarios.size} failed, #@pending_scenario_count pending"
if @failed_scenarios.size > 0
  out = red(out)
else 
  if @pending_scenario_count > 0
    out = yellow(out)
  else
    out = green(out)
  end
end
@output.puts out 
#COLORIZE! END
#@output.puts "#@count scenarios: #@successful_scenario_count succeeded, #{@failed_scenarios.size} failed, #@pending_scenario_count pending"

Enjoy the colors 😀

RSpec: Testing Mail Delivery (simple)

To really test mail delivery one needs to send and then grab via smtp, but i do not want to go this far atm. So i wrote a surprisingly simple test, that satisfies my need for coverage and be done with it!

If you messed around with your environment.rb and are not sure wheter emails are send in test mode, just enter a real email and watch the inbox, before sending mass-emails…

#spec/models/user_mailer_spec.rb
describe UserMailer do
  fixtures :users

  before(:each) do
    ActionMailer::Base.deliveries = []
  end

  it 'should send activation' do
    UserMailer.deliver_activation(users(:quentin))
    sent.first.subject.should =~ /has been activated/#correct subject
    sent.first.body.should =~ /#{CFG[:site]}/#url to our site is there
  end

  def sent
    ActionMailer::Base.deliveries
  end
end