Giter Site home page Giter Site logo

sinatra-unit's Introduction

# sinatra-unit

* http://github.com/snarfmason/sinatra-unit

Testing harness for Sinatra to make proper unit tests/specs for actions.

Description
-----------

The idea is to instantiate your Sintra class and then execute the web request blocks on the instance
that you have created. It uses the `test_request` method (and shortcuts like `test_get`, `test_put`, etc)
to look up the block using the same route matching logic that Sinatra would use when receiving a request
from Rack.

Usage
-----

Basic usage:

    @app = WebApp.new
    response_body = @app.test_request(:get, '/')

of course you can do, params:

    response_body = @app.test_request(:get, '/hi', :qsname => 'jon')

you can set the environment or session (if enabled) with a hash, before a request

    @app.env = { :test => 'hello' }
    @app.session = { :test => 'hello' }
    response_body = @app.test_get '/showsession'

Currently the text of the response is returned from the `test_request` method. If you need access to headers or HTTP status you use the `response` object.

    @app.response.status
    @app.response.header['Content-Type']


Writing Tests
-------------

basic good route:

    response_body = @app.test_request(:get, '/')
    assert_equal "hello world", response_body
    assert_equal 200, @app.response.status
    assert_equal 'text/html', @app.response.header['Content-Type']

basic bad route:

    assert_raise Sinatra::Unit::UnknownRouteError do
      @app.test_get '/wrongroute'
    end

basic redirect:

    @app.test_get '/redirect'
    assert @app.redirect?
    assert_equal 302, @app.response.status

you can retrieve instance variables set in request blocks

    # app
    get '/showobject' do
      @object = OpenStruct.new :foo => 1, :bar => 2, :baz => 3
      "foo=#{@object.foo}&bar=#{@object.bar}&baz=#{@object.baz}"
    end

    # test
    response_body = @app.test_get '/showobject'
    assert_equal 'foo=1&bar=2&baz=3', response_body
    assert_equal 1, @app.variables("object").foo

you can assert against session values

    # app
    post '/setsession' do
      session[:test] = params[:test]
    end
    
    # test
    assert_nil @app.session[:test]
    @app.test_post '/setsession', :test => 'goodbye'
    assert_equal 'goodbye', @app.session[:test]

sinatra-unit's People

Contributors

snarfmason avatar srbaker avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.