Sha256: 5d02c36639831371cb5901f2c29c095ef892543ac62b75cdc3dfe7d4b3178321

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

require File.expand_path('../test_helper', __FILE__)

DB.create_table :books do
  primary_key :id
  String :name
  String :author
end

class Book < Sequel::Model
  def to_hash
    { :name => name, :author => author }
  end

  def validate
    super
    errors.add(:author, "can't be empty")  if author.to_s.size == 0
  end
end

class AppTest < UnitTest
  class App < Sinatra::Base
    register Sinatra::RestAPI
    disable :show_exceptions
    enable :raise_errors
    rest_create("/book") { Book.new }
    rest_resource("/book/:id") { |id| Book[id] }
  end
  def app() App; end

  describe "Sinatra::RestAPI" do
    setup do
      @book = Book.new
      @book.name   = "Darkly Dreaming Dexter"
      @book.author = "Jeff Lindsay"
      @book.save
      header 'Accept', 'application/json, */*'
    end

    teardown do
      @book.destroy  if Book[@book.id]
    end

    test "should work properly" do
      get "/book/#{@book.id}"

      assert json_response['name']   == @book.name
      assert json_response['author'] == @book.author
    end

    test "validation fail" do
      hash = { :name => "The Claiming of Sleeping Beauty" }
      post "/book", :model => hash.to_json
      assert last_response.status != 200
    end

    test "should 404" do
      get "/book/823978"

      assert last_response.status == 404
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
sinatra-backbone-2-0.1.1 test/app_test.rb
sinatra-backbone-0.1.1 test/app_test.rb