Skip to content

Commit

Permalink
add test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
achiurizo committed Jun 8, 2011
1 parent 98992c0 commit 4239a65
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ gem 'bson_ext', :require => "mongo"

# Test requirements
group :test do
gem 'database_cleaner'
gem 'machinist'
gem 'machinist_mongo', :require => 'machinist/mongoid'
gem 'rr'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ GEM
bson (1.3.1)
bson_ext (1.3.1)
builder (2.1.2)
database_cleaner (0.6.7)
diff-lcs (1.1.2)
faker (0.9.5)
i18n (~> 0.4)
Expand Down Expand Up @@ -98,6 +99,7 @@ PLATFORMS

DEPENDENCIES
bson_ext
database_cleaner
faker
haml
machinist
Expand Down
3 changes: 3 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require File.expand_path('../config/boot.rb', __FILE__)
require 'padrino-core/cli/rake'
PadrinoTasks.init
43 changes: 43 additions & 0 deletions test.watchr
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# install watchr
# $ gem install watchr
#
# Run With:
# $ watchr test.watchr
#

# --------------------------------------------------
# Helpers
# --------------------------------------------------

def run(cmd)
puts(cmd)
system("clear")
system("ruby #{cmd}")
end

# --------------------------------------------------
# Watchr Rules
# --------------------------------------------------
watch("^lib/(.*)\.rb") { |m| run("test/#{m[1]}_test.rb") }

watch("^(.*)/controllers/(.*).rb") { |m| run("test/#{m[1]}/controllers/#{m[2]}_controller_test.rb") }
watch("^test/(.*)/controllers/(.*)_test.rb") { |m| run("test/#{m[1]}/controllers/#{m[2]}_test.rb") }

watch("^(.*)/models/(.*).rb") { |m| run("test/#{m[1]}/models/#{m[2]}_test.rb") }
watch("^test/(.*)/(.*)_test.rb") { |m| run("test/#{m[1]}/models/#{m[2]}_test.rb") }

watch("test.*/test_config\.rb") { system( "padrino rake test" ) }
watch("^test/(.*)_test\.rb") { |m| run("test/#{m[1]}_test.rb") }

# --------------------------------------------------
# Signal Handling
# --------------------------------------------------
# Ctrl-\
Signal.trap('QUIT') do
system 'clear'
puts " --- Running all tests ---\n\n"
system "padrino rake test"
end

# Ctrl-C
Signal.trap('INT') { abort("\n") }
8 changes: 8 additions & 0 deletions test/blueprints.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
Sham.define do
username { Faker::Internet.user_name }
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
password { (1..10).map { ('a'..'z').to_a.rand }.join }
email { Faker::Internet.email }
title { Faker::Lorem.words(3).join(' ') }
body { Faker::Lorem.words(8).join(' ') }
tag_name { Faker::Lorem.words(1).join }
end
19 changes: 19 additions & 0 deletions test/support/database_cleaner_middleware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class DatabaseCleanerMiddleware < Riot::ContextMiddleware
register

def call(context)
setup_database_cleaner(context) if clean_database?(context)
middleware.call(context)
end

private

def clean_database?(context)
context.option(:clean_database)
end

def setup_database_cleaner(context)
context.setup { DatabaseCleaner.clean }
end
end

3 changes: 2 additions & 1 deletion test/test.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'rake/testtask'

test_tasks = Dir['test/*/'].map { |d| File.basename(d) }
test_tasks = Dir['test/*/'].map { |d| File.basename(d) }.reject { |d| d =~ /support/ }

test_tasks.each do |folder|
Rake::TestTask.new("test:#{folder}") do |test|
Expand All @@ -11,3 +11,4 @@ end

desc "Run application test suite"
task 'test' => test_tasks.map { |f| "test:#{f}" }

14 changes: 13 additions & 1 deletion test/test_config.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
PADRINO_ENV = 'test' unless defined?(PADRINO_ENV)
require File.expand_path(File.dirname(__FILE__) + "/../config/boot")
require File.join(File.dirname(__FILE__),'blueprints')
require 'riot/rr'

Riot.pretty_dots

DatabaseCleaner.strategy = :truncation

# Support Files
Dir.glob(File.expand_path('../support/*.rb',__FILE__)).each { |f| require f }


# Specify your app using the #app helper inside a context.
# Takes either an app class or a block argument.
# app { Padrino.application }
# app { Shortener.tap { |app| } }

class Riot::Situation
include Rack::Test::Methods
##
Expand Down

0 comments on commit 4239a65

Please sign in to comment.