bdd snippets

Test functionality in isolation with Autospec

Tagged autotest, testing, bdd, tdd, autospec, zentest  Languages ruby

This configuration will make autospec run "spec/models/xyz_spec" when a file in "lib/xyz/ is modified". No other tests are run.

Put the following code in ./.autotest:

Autotest.add_hook :initialize do |at|
  at.clear_mappings
  %w{.svn .hg .git vendor}.each {|exception| at.add_exception(exception)}

  at.add_mapping(%r%^lib/xyz/.*\.rb$%) {
    at.files_matching %r%^spec/models/xyz_spec\.rb$%
#
# Uncomment if more tests are needed...
# +   at.files_matching %r%^spec/models/xyz_spec\.rb$%
#
  }
end

How to test a Sinatra webapp with Rspec

Tagged sinatra, rspec, spec, testing, bdd  Languages ruby

Create spec/app_spec.rb and put the following in it:

require 'sinatra'
require 'spec/interop/test'
require 'sinatra/test/unit'

require 'app'
require 'test/spec'

set :environment, :test

describe 'The HelloWorld App' do
  before(:each) do
    set :views => File.join(File.dirname(__FILE__), "..", "views")
  end

  it "says hello" do
    get '/'
    response.should.be.ok
    response.body.should.equal 'Hello World'
  end
end

Then run the test:

spec spec/app_spec.rb