socialcast_shoulda_ext 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/Gemfile +13 -0
- data/MIT-LICENSE +22 -0
- data/README.rdoc +126 -0
- data/Rakefile +12 -0
- data/lib/shoulda_ext.rb +7 -0
- data/lib/shoulda_ext/assertions.rb +14 -0
- data/lib/shoulda_ext/integrations/test_unit.rb +13 -0
- data/lib/shoulda_ext/matchers.rb +9 -0
- data/lib/shoulda_ext/matchers/record_count_change.rb +127 -0
- data/lib/shoulda_ext/matchers/respond_with_json.rb +99 -0
- data/lib/shoulda_ext/matchers/trigger_callback.rb +131 -0
- data/lib/shoulda_ext/shoulda_patches/context_with_matcher_before_hooks.rb +32 -0
- data/lib/shoulda_ext/version.rb +4 -0
- data/socialcast_shoulda_ext.gemspec +22 -0
- data/test/helper.rb +30 -0
- data/test/rails3_root/.gitignore +4 -0
- data/test/rails3_root/Rakefile +7 -0
- data/test/rails3_root/app/controllers/application_controller.rb +3 -0
- data/test/rails3_root/app/controllers/blogs_controller.rb +11 -0
- data/test/rails3_root/app/controllers/comments_controller.rb +2 -0
- data/test/rails3_root/app/helpers/application_helper.rb +2 -0
- data/test/rails3_root/app/helpers/blogs_helper.rb +2 -0
- data/test/rails3_root/app/helpers/comments_helper.rb +2 -0
- data/test/rails3_root/app/models/blog.rb +3 -0
- data/test/rails3_root/app/models/comment.rb +3 -0
- data/test/rails3_root/app/views/blogs/index.html.erb +6 -0
- data/test/rails3_root/app/views/layouts/application.html.erb +14 -0
- data/test/rails3_root/config.ru +4 -0
- data/test/rails3_root/config/application.rb +17 -0
- data/test/rails3_root/config/boot.rb +6 -0
- data/test/rails3_root/config/database.yml +22 -0
- data/test/rails3_root/config/environment.rb +5 -0
- data/test/rails3_root/config/environments/development.rb +26 -0
- data/test/rails3_root/config/environments/production.rb +49 -0
- data/test/rails3_root/config/environments/test.rb +35 -0
- data/test/rails3_root/config/initializers/backtrace_silencers.rb +7 -0
- data/test/rails3_root/config/initializers/inflections.rb +10 -0
- data/test/rails3_root/config/initializers/mime_types.rb +5 -0
- data/test/rails3_root/config/initializers/secret_token.rb +7 -0
- data/test/rails3_root/config/initializers/session_store.rb +8 -0
- data/test/rails3_root/config/locales/en.yml +5 -0
- data/test/rails3_root/config/routes.rb +4 -0
- data/test/rails3_root/db/migrate/20110411025326_create_blogs.rb +12 -0
- data/test/rails3_root/db/migrate/20110411025332_create_comments.rb +13 -0
- data/test/rails3_root/db/seeds.rb +7 -0
- data/test/rails3_root/lib/tasks/.gitkeep +0 -0
- data/test/rails3_root/public/404.html +26 -0
- data/test/rails3_root/public/422.html +26 -0
- data/test/rails3_root/public/500.html +26 -0
- data/test/rails3_root/public/favicon.ico +0 -0
- data/test/rails3_root/public/images/rails.png +0 -0
- data/test/rails3_root/public/index.html +239 -0
- data/test/rails3_root/public/javascripts/application.js +2 -0
- data/test/rails3_root/public/javascripts/controls.js +965 -0
- data/test/rails3_root/public/javascripts/dragdrop.js +974 -0
- data/test/rails3_root/public/javascripts/effects.js +1123 -0
- data/test/rails3_root/public/javascripts/prototype.js +6001 -0
- data/test/rails3_root/public/javascripts/rails.js +191 -0
- data/test/rails3_root/public/robots.txt +5 -0
- data/test/rails3_root/public/stylesheets/.gitkeep +0 -0
- data/test/rails3_root/script/rails +6 -0
- data/test/rails3_root/test/fixtures/blogs.yml +5 -0
- data/test/rails3_root/test/fixtures/comments.yml +0 -0
- data/test/rails3_root/test/functional/blogs_controller_test.rb +8 -0
- data/test/rails3_root/test/functional/comments_controller_test.rb +8 -0
- data/test/rails3_root/test/shoulda_ext.sqlite3 +0 -0
- data/test/rails3_root/test/test_helper.rb +13 -0
- data/test/rails3_root/test/unit/blog_test.rb +8 -0
- data/test/rails3_root/test/unit/comment_test.rb +8 -0
- data/test/rails3_root/test/unit/helpers/blogs_helper_test.rb +4 -0
- data/test/rails3_root/test/unit/helpers/comments_helper_test.rb +4 -0
- data/test/rails3_root/vendor/plugins/.gitkeep +0 -0
- data/test/test_assertions.rb +14 -0
- data/test/test_record_count_change_matcher.rb +150 -0
- data/test/test_respond_with_json.rb +42 -0
- data/test/test_trigger_callback_matcher.rb +38 -0
- metadata +362 -0
@@ -0,0 +1,131 @@
|
|
1
|
+
module ShouldaExt # :nodoc:
|
2
|
+
module Matchers # :nodoc:
|
3
|
+
|
4
|
+
CALLBACK_EVENTS = [:before, :after, :after_commit_on]
|
5
|
+
CALLBACK_TYPES = [:create, :update, :destroy, :save]
|
6
|
+
|
7
|
+
# Test if create, update, destroy, or save callbacks were triggered.
|
8
|
+
# Examples:
|
9
|
+
#
|
10
|
+
# context "doing nothing to a record" do
|
11
|
+
# subject { Blog.new :title => 'blog title' }
|
12
|
+
# should_not trigger_callbacks
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# context "creating a record" do
|
16
|
+
# subject { Blog.create! :title => 'blog title' }
|
17
|
+
# should trigger_callbacks.for :create
|
18
|
+
# should_not trigger_callbacks.for :update, :destroy
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
def trigger_callbacks
|
22
|
+
TriggerCallbackMatcher.new.any
|
23
|
+
end
|
24
|
+
|
25
|
+
class TriggerCallbackMatcher
|
26
|
+
module ActiveRecordHooks # :nodoc:
|
27
|
+
|
28
|
+
def self.included(base)
|
29
|
+
class << base
|
30
|
+
def inherited_with_hooks(subclass)
|
31
|
+
inherited_without_hooks(subclass)
|
32
|
+
ActiveRecordHooks.attach_to subclass
|
33
|
+
end
|
34
|
+
alias_method_chain :inherited, :hooks
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.attach_to(model)
|
39
|
+
puts "Attaching trigger_callback test hooks in #{model.name}"
|
40
|
+
class << model
|
41
|
+
attr_accessor :callback_tester_attrs
|
42
|
+
end
|
43
|
+
model.class_eval do
|
44
|
+
@callback_tester_attrs = []
|
45
|
+
CALLBACK_EVENTS.each do |ce|
|
46
|
+
CALLBACK_TYPES.each do |ct|
|
47
|
+
callback_name = :"#{ce}_#{ct}"
|
48
|
+
callback_attr = :"called_#{callback_name}"
|
49
|
+
callback_method, has_on_option = (ce.to_s =~ /_on/ ? [ce.to_s.gsub('_on',''), true] : [callback_name, false])
|
50
|
+
@callback_tester_attrs << callback_attr
|
51
|
+
attr_accessor callback_attr
|
52
|
+
send( callback_method, (has_on_option ? {:on => ct} : {})) {
|
53
|
+
instance_variable_set(:"@#{callback_attr}", true)
|
54
|
+
}
|
55
|
+
|
56
|
+
define_method :"#{callback_attr}?" do
|
57
|
+
!!instance_variable_get(:"@#{callback_attr}")
|
58
|
+
end
|
59
|
+
end # - each
|
60
|
+
end # - each
|
61
|
+
end # - class_eval
|
62
|
+
end
|
63
|
+
|
64
|
+
def reset_callback_flags!
|
65
|
+
self.class.callback_tester_attrs.each do |attr|
|
66
|
+
send("#{attr}=", false)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end # - ActiveRecordHooks
|
71
|
+
|
72
|
+
# Attach the ActiveRecord callback hooks needed for using
|
73
|
+
# the trigger_callbacks matcher
|
74
|
+
def self.attach_active_record_callback_hooks!
|
75
|
+
puts "Attaching callback hooks into ActiveRecord"
|
76
|
+
ActiveRecord::Base.descendants.each do |model|
|
77
|
+
TriggerCallbackMatcher::ActiveRecordHooks.attach_to model
|
78
|
+
end
|
79
|
+
ActiveRecord::Base.send :include, TriggerCallbackMatcher::ActiveRecordHooks
|
80
|
+
end
|
81
|
+
|
82
|
+
# Define the set of callback types (create, update, destroy, save) to test
|
83
|
+
def for(*callback_types)
|
84
|
+
@callback_types = Array.wrap(callback_types)
|
85
|
+
@any_callbacks = false
|
86
|
+
self
|
87
|
+
end
|
88
|
+
|
89
|
+
# Check to see if any of the callback types have been triggered
|
90
|
+
def any
|
91
|
+
@callback_types = CALLBACK_TYPES
|
92
|
+
@any_callbacks = true
|
93
|
+
self
|
94
|
+
end
|
95
|
+
|
96
|
+
def failure_message
|
97
|
+
"Expected #{@subject} #{expectation}:"
|
98
|
+
end
|
99
|
+
|
100
|
+
def negative_failure_message
|
101
|
+
"Did not expect #{@subject} #{expectation}:"
|
102
|
+
end
|
103
|
+
|
104
|
+
def description
|
105
|
+
"check that #{@callback_types.join(', ')} callbacks were called"
|
106
|
+
end
|
107
|
+
|
108
|
+
def expectation
|
109
|
+
@expectations.join("\n")
|
110
|
+
end
|
111
|
+
|
112
|
+
def matches?(subject)
|
113
|
+
@subject = subject
|
114
|
+
@expectations = []
|
115
|
+
result = !@any_callbacks
|
116
|
+
@callback_types.each do |ct|
|
117
|
+
CALLBACK_EVENTS.each do |ce|
|
118
|
+
called = @subject.send(:"called_#{ce}_#{ct}?")
|
119
|
+
if @any_callbacks
|
120
|
+
result ||= called
|
121
|
+
else
|
122
|
+
result &&= called
|
123
|
+
end
|
124
|
+
@expectations << "#{ce}_#{ct} callbacks to be triggered"
|
125
|
+
end
|
126
|
+
end
|
127
|
+
result
|
128
|
+
end
|
129
|
+
end # - TriggerCallbackMatcher
|
130
|
+
end # - Matchers
|
131
|
+
end # - ShouldaExt
|
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
# Adds the ability to define a 'before' method on a matcher and allow
|
3
|
+
# it to be run before the subject/setup phase of the current context
|
4
|
+
#
|
5
|
+
# Pull Request for getting this functionality into shoulda-context
|
6
|
+
# https://github.com/thoughtbot/shoulda-context/pull/2
|
7
|
+
module Shoulda
|
8
|
+
class Context
|
9
|
+
def should(name_or_matcher, options = {}, &blk)
|
10
|
+
if name_or_matcher.respond_to?(:description) && name_or_matcher.respond_to?(:matches?)
|
11
|
+
name = name_or_matcher.description
|
12
|
+
blk = lambda { assert_accepts name_or_matcher, subject }
|
13
|
+
options[:before] = lambda { name_or_matcher.before } if name_or_matcher.respond_to?(:before)
|
14
|
+
else
|
15
|
+
name = name_or_matcher
|
16
|
+
end
|
17
|
+
|
18
|
+
if blk
|
19
|
+
self.shoulds << { :name => name, :before => options[:before], :block => blk }
|
20
|
+
else
|
21
|
+
self.should_eventuallys << { :name => name }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def should_not(matcher)
|
26
|
+
name = matcher.description
|
27
|
+
blk = lambda { assert_rejects matcher, subject }
|
28
|
+
before = matcher.respond_to?(:before) ? lambda { matcher.before } : nil
|
29
|
+
self.shoulds << { :name => "not #{name}", :block => blk, :before => before }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "shoulda_ext/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "socialcast_shoulda_ext"
|
7
|
+
s.version = ShouldaExt::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Geoffrey Hichborn"]
|
10
|
+
s.email = ["geoff@socialcast.com"]
|
11
|
+
s.homepage = "http://github.com/socialcast/socialcast-shoulda-ext"
|
12
|
+
s.summary = %q{adds new shoulda matchers and assertions}
|
13
|
+
s.description = File.read('README.rdoc')
|
14
|
+
|
15
|
+
s.add_runtime_dependency(%q<activerecord>, ["~> 3.0.0"])
|
16
|
+
s.add_runtime_dependency(%q<shoulda>, [">= 2.11.1"])
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
ENV['RAILS_ENV'] = 'test'
|
2
|
+
|
3
|
+
# Load Rails 3 instance
|
4
|
+
rails_root = File.join(File.dirname(__FILE__), 'rails3_root')
|
5
|
+
ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__)
|
6
|
+
require "#{rails_root}/config/environment.rb"
|
7
|
+
require 'rails/test_help'
|
8
|
+
|
9
|
+
# Load shoulda extensions
|
10
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
11
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
12
|
+
require 'shoulda_ext'
|
13
|
+
|
14
|
+
# Activate ActiveRecord hooks for trigger_callbacks matcher
|
15
|
+
ShouldaExt::Matchers::TriggerCallbackMatcher.attach_active_record_callback_hooks!
|
16
|
+
|
17
|
+
# Run the migrations
|
18
|
+
ActiveRecord::Migration.verbose = false
|
19
|
+
ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate")
|
20
|
+
|
21
|
+
# Setup the fixtures path
|
22
|
+
ActiveSupport::TestCase.fixture_path =
|
23
|
+
File.join(File.dirname(__FILE__), "rails3_root/test/fixtures")
|
24
|
+
|
25
|
+
class ActiveSupport::TestCase #:nodoc:
|
26
|
+
self.use_transactional_fixtures = false
|
27
|
+
self.use_instantiated_fixtures = false
|
28
|
+
end
|
29
|
+
|
30
|
+
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
2
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
3
|
+
|
4
|
+
require File.expand_path('../config/application', __FILE__)
|
5
|
+
require 'rake'
|
6
|
+
|
7
|
+
Rails3Root::Application.load_tasks
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path('../boot', __FILE__)
|
2
|
+
|
3
|
+
require 'rails/all'
|
4
|
+
|
5
|
+
# If you have a Gemfile, require the gems listed there, including any gems
|
6
|
+
# you've limited to :test, :development, or :production.
|
7
|
+
Bundler.require(:default, Rails.env) if defined?(Bundler)
|
8
|
+
|
9
|
+
module Rails3Root
|
10
|
+
class Application < Rails::Application
|
11
|
+
# Configure the default encoding used in templates for Ruby 1.9.
|
12
|
+
config.encoding = "utf-8"
|
13
|
+
|
14
|
+
# Configure sensitive parameters which will be filtered from the log file.
|
15
|
+
config.filter_parameters += [:password]
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# SQLite version 3.x
|
2
|
+
# gem install sqlite3
|
3
|
+
development:
|
4
|
+
adapter: sqlite3
|
5
|
+
database: db/development.sqlite3
|
6
|
+
pool: 5
|
7
|
+
timeout: 5000
|
8
|
+
|
9
|
+
# Warning: The database defined as "test" will be erased and
|
10
|
+
# re-generated from your development database when you run "rake".
|
11
|
+
# Do not set this db to the same as development or production.
|
12
|
+
test:
|
13
|
+
adapter: sqlite3
|
14
|
+
database: db/test.sqlite3
|
15
|
+
pool: 5
|
16
|
+
timeout: 5000
|
17
|
+
|
18
|
+
production:
|
19
|
+
adapter: sqlite3
|
20
|
+
database: db/production.sqlite3
|
21
|
+
pool: 5
|
22
|
+
timeout: 5000
|
@@ -0,0 +1,26 @@
|
|
1
|
+
Rails3Root::Application.configure do
|
2
|
+
# Settings specified here will take precedence over those in config/application.rb
|
3
|
+
|
4
|
+
# In the development environment your application's code is reloaded on
|
5
|
+
# every request. This slows down response time but is perfect for development
|
6
|
+
# since you don't have to restart the webserver when you make code changes.
|
7
|
+
config.cache_classes = false
|
8
|
+
|
9
|
+
# Log error messages when you accidentally call methods on nil.
|
10
|
+
config.whiny_nils = true
|
11
|
+
|
12
|
+
# Show full error reports and disable caching
|
13
|
+
config.consider_all_requests_local = true
|
14
|
+
config.action_view.debug_rjs = true
|
15
|
+
config.action_controller.perform_caching = false
|
16
|
+
|
17
|
+
# Don't care if the mailer can't send
|
18
|
+
config.action_mailer.raise_delivery_errors = false
|
19
|
+
|
20
|
+
# Print deprecation notices to the Rails logger
|
21
|
+
config.active_support.deprecation = :log
|
22
|
+
|
23
|
+
# Only use best-standards-support built into browsers
|
24
|
+
config.action_dispatch.best_standards_support = :builtin
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,49 @@
|
|
1
|
+
Rails3Root::Application.configure do
|
2
|
+
# Settings specified here will take precedence over those in config/application.rb
|
3
|
+
|
4
|
+
# The production environment is meant for finished, "live" apps.
|
5
|
+
# Code is not reloaded between requests
|
6
|
+
config.cache_classes = true
|
7
|
+
|
8
|
+
# Full error reports are disabled and caching is turned on
|
9
|
+
config.consider_all_requests_local = false
|
10
|
+
config.action_controller.perform_caching = true
|
11
|
+
|
12
|
+
# Specifies the header that your server uses for sending files
|
13
|
+
config.action_dispatch.x_sendfile_header = "X-Sendfile"
|
14
|
+
|
15
|
+
# For nginx:
|
16
|
+
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
|
17
|
+
|
18
|
+
# If you have no front-end server that supports something like X-Sendfile,
|
19
|
+
# just comment this out and Rails will serve the files
|
20
|
+
|
21
|
+
# See everything in the log (default is :info)
|
22
|
+
# config.log_level = :debug
|
23
|
+
|
24
|
+
# Use a different logger for distributed setups
|
25
|
+
# config.logger = SyslogLogger.new
|
26
|
+
|
27
|
+
# Use a different cache store in production
|
28
|
+
# config.cache_store = :mem_cache_store
|
29
|
+
|
30
|
+
# Disable Rails's static asset server
|
31
|
+
# In production, Apache or nginx will already do this
|
32
|
+
config.serve_static_assets = false
|
33
|
+
|
34
|
+
# Enable serving of images, stylesheets, and javascripts from an asset server
|
35
|
+
# config.action_controller.asset_host = "http://assets.example.com"
|
36
|
+
|
37
|
+
# Disable delivery errors, bad email addresses will be ignored
|
38
|
+
# config.action_mailer.raise_delivery_errors = false
|
39
|
+
|
40
|
+
# Enable threaded mode
|
41
|
+
# config.threadsafe!
|
42
|
+
|
43
|
+
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
|
44
|
+
# the I18n.default_locale when a translation can not be found)
|
45
|
+
config.i18n.fallbacks = true
|
46
|
+
|
47
|
+
# Send deprecation notices to registered listeners
|
48
|
+
config.active_support.deprecation = :notify
|
49
|
+
end
|