How to test a Sinatra webapp with Rspec

Ruby posted 11 months ago by christian

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

   1  require 'sinatra'
   2  require 'spec/interop/test'
   3  require 'sinatra/test/unit'
   4  
   5  require 'app'
   6  require 'test/spec'
   7  
   8  set :environment, :test
   9  
  10  describe 'The HelloWorld App' do
  11    before(:each) do
  12      set :views => File.join(File.dirname(__FILE__), "..", "views")
  13    end
  14  
  15    it "says hello" do
  16      get '/'
  17      response.should.be.ok
  18      response.body.should.equal 'Hello World'
  19    end
  20  end

Then run the test:

   1  spec spec/app_spec.rb

Tagged sinatra, rspec, spec, testing, bdd

Test functionality in isolation with Autospec

Ruby posted about 1 year ago by christian

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:

   1  Autotest.add_hook :initialize do |at|
   2    at.clear_mappings
   3    %w{.svn .hg .git vendor}.each {|exception| at.add_exception(exception)}
   4  
   5    at.add_mapping(%r%^lib/xyz/.*\.rb$%) {
   6      at.files_matching %r%^spec/models/xyz_spec\.rb$%
   7  #
   8  # Uncomment if more tests are needed...
   9  # +   at.files_matching %r%^spec/models/xyz_spec\.rb$%
  10  #
  11    }
  12  end

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