Sha256: 5f75f6218e8bcf80d8d2f548e525a419a7bb8c54f954c32d12106e44b2ab6a7a

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 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
      p last_response
    end

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

      assert last_response.status == 404
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sinatra-backbone-0.1.0.rc2 test/app_test.rb