Skip to content
nesquena edited this page Sep 13, 2010 · 8 revisions

This section covers the specifics of setting up an application using the sinatra_more generators and each test component.

bacon

In order to use bacon, you must install the associated gem:

$ sudo gem install bacon --source http://gemcutter.org

To generate an application using the bacon testing framework, simply invoke:

sinatra_gen demo_app . -t bacon

This will setup the test/test_config.rb file to use bacon and include the necessary modules to make testing easy. In order to add tests, simply create a test file within test/models or test/routes in order to get started testing a piece of the application.

# test/routes/app_test.rb
require File.dirname(__FILE__) + '/../test_config.rb'

describe "Basic Application" do
  it 'returns hello world at root' do
    get '/'
    last_response.body.should == "Hello World"
  end
end

This does a basic test of the routes by requesting the root url and then ensuring that the response body matches the expected content.

You can execute the test file by invoking:

$ bacon test/routes/app_test.rb

For more information on using bacon, check out the Bacon README

shoulda

In order to use shoulda, you must install the associated gem:

$ sudo gem install shoulda --source http://gemcutter.org

To generate an application using the shoulda testing framework, simply invoke:

sinatra_gen demo_app . -t shoulda

This will setup the test/test_config.rb file to use shoulda and include the necessary modules to make testing easy. In order to add tests, simply create a test file within test/models or test/routes in order to get started testing a piece of the application.

# test/routes/app_test.rb
require File.dirname(__FILE__) + '/../test_config.rb'

class PostsControllerTest < Test::Unit::TestCase
  context "on GET to root" do
    setup do
      get '/'
    end

    should "return hello world text" do
      assert_equal "Hello World", last_response.body
    end
  end
end

This does a basic test of the routes by requesting the root url and then ensuring that the response body matches the expected content.

You can execute the test file by invoking:

$ ruby test/routes/app_test.rb

For more information on using shoulda, check out the Shoulda README

riot

In order to use riot, you must install the associated gem:

$ sudo gem install riot --source http://gemcutter.org

To generate an application using the riot testing framework, simply invoke:

sinatra_gen demo_app . -t riot

This will setup the test/test_config.rb file to use riot and include the necessary modules to make testing easy. In order to add tests, simply create a test file within test/models or test/routes in order to get started testing a piece of the application.

# test/routes/app_test.rb
require File.dirname(__FILE__) + '/../test_config.rb'

context 'Basic Application' do
  context "/index" do
    setup { get '/' }
    asserts("the response body") { last_response.body }.equals "Hello World"
  end
end

This does a basic test of the routes by requesting the root url and then ensuring that the response body matches the expected content.

You can execute the test file by invoking:

$ ruby test/routes/app_test.rb

For more information on using riot, check out the Riot README

testspec

In order to use testspec, you must install the associated gem:

$ sudo gem install test-spec --source http://gemcutter.org

To generate an application using the test/spec testing framework, simply invoke:

sinatra_gen demo_app . -t testspec

This will setup the test/test_config.rb file to use testspec and include the necessary modules to make testing easy. In order to add tests, simply create a test file within test/models or test/routes in order to get started testing a piece of the application.

# test/routes/app_test.rb
require File.dirname(__FILE__) + '/../test_config.rb'

context "for root url" do
  setup { get('/') }
  specify "returns hello world" do
    last_response.body.should.equal "Hello World"
  end
end

This does a basic test of the routes by requesting the root url and then ensuring that the response body matches the expected content.

You can execute the test file by invoking:

$ ruby test/routes/app_test.rb

For more information on using shoulda, check out the TestSpec README

rspec

In order to use rspec, you must install the associated gem:

$ sudo gem install rspec --source http://gemcutter.org

To generate an application using the rspec testing framework, simply invoke:

sinatra_gen demo_app . -t rspec

This will setup the test/test_config.rb file to use rspec and include the necessary modules to make testing easy. In order to add tests, simply create a test file within test/models or test/routes in order to get started testing a piece of the application.

# test/routes/app_test.rb
require File.dirname(__FILE__) + '/../test_config.rb'

describe "for root url" do
  setup { get('/') }
  it "returns hello world" do
    last_response.body.should == "Hello World"
  end
end

This does a basic test of the routes by requesting the root url and then ensuring that the response body matches the expected content.

You can execute the test file by invoking:

$ spec test/routes/app_test.rb

For more information on using rspec, check out the Rspec README

Clone this wiki locally