technicalpickles-shoulda 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CONTRIBUTION_GUIDELINES.rdoc +12 -0
- data/MIT-LICENSE +22 -0
- data/README.rdoc +132 -0
- data/Rakefile +72 -0
- data/bin/convert_to_should_syntax +42 -0
- data/lib/shoulda/action_mailer/assertions.rb +39 -0
- data/lib/shoulda/action_mailer.rb +10 -0
- data/lib/shoulda/active_record/assertions.rb +84 -0
- data/lib/shoulda/active_record/macros.rb +684 -0
- data/lib/shoulda/active_record.rb +12 -0
- data/lib/shoulda/assertions.rb +45 -0
- data/lib/shoulda/context.rb +309 -0
- data/lib/shoulda/controller/formats/html.rb +201 -0
- data/lib/shoulda/controller/formats/xml.rb +170 -0
- data/lib/shoulda/controller/helpers.rb +64 -0
- data/lib/shoulda/controller/macros.rb +171 -0
- data/lib/shoulda/controller/resource_options.rb +236 -0
- data/lib/shoulda/controller/routing/macros.rb +47 -0
- data/lib/shoulda/controller/routing.rb +10 -0
- data/lib/shoulda/controller.rb +31 -0
- data/lib/shoulda/helpers.rb +10 -0
- data/lib/shoulda/macros.rb +80 -0
- data/lib/shoulda/private_helpers.rb +22 -0
- data/lib/shoulda/proc_extensions.rb +14 -0
- data/lib/shoulda/rails.rb +19 -0
- data/lib/shoulda/tasks/list_tests.rake +24 -0
- data/lib/shoulda/tasks/yaml_to_shoulda.rake +28 -0
- data/lib/shoulda/tasks.rb +3 -0
- data/lib/shoulda.rb +16 -0
- data/test/README +36 -0
- data/test/fixtures/addresses.yml +3 -0
- data/test/fixtures/friendships.yml +0 -0
- data/test/fixtures/posts.yml +5 -0
- data/test/fixtures/products.yml +0 -0
- data/test/fixtures/taggings.yml +0 -0
- data/test/fixtures/tags.yml +9 -0
- data/test/fixtures/users.yml +6 -0
- data/test/functional/posts_controller_test.rb +72 -0
- data/test/functional/users_controller_test.rb +36 -0
- data/test/other/context_test.rb +145 -0
- data/test/other/convert_to_should_syntax_test.rb +63 -0
- data/test/other/helpers_test.rb +183 -0
- data/test/other/private_helpers_test.rb +34 -0
- data/test/other/should_test.rb +266 -0
- data/test/rails_root/app/controllers/application.rb +25 -0
- data/test/rails_root/app/controllers/posts_controller.rb +78 -0
- data/test/rails_root/app/controllers/users_controller.rb +81 -0
- data/test/rails_root/app/helpers/application_helper.rb +3 -0
- data/test/rails_root/app/helpers/posts_helper.rb +2 -0
- data/test/rails_root/app/helpers/users_helper.rb +2 -0
- data/test/rails_root/app/models/address.rb +7 -0
- data/test/rails_root/app/models/dog.rb +5 -0
- data/test/rails_root/app/models/flea.rb +3 -0
- data/test/rails_root/app/models/friendship.rb +4 -0
- data/test/rails_root/app/models/post.rb +12 -0
- data/test/rails_root/app/models/product.rb +12 -0
- data/test/rails_root/app/models/tag.rb +8 -0
- data/test/rails_root/app/models/tagging.rb +4 -0
- data/test/rails_root/app/models/user.rb +28 -0
- data/test/rails_root/app/views/layouts/posts.rhtml +17 -0
- data/test/rails_root/app/views/layouts/users.rhtml +17 -0
- data/test/rails_root/app/views/posts/edit.rhtml +27 -0
- data/test/rails_root/app/views/posts/index.rhtml +25 -0
- data/test/rails_root/app/views/posts/new.rhtml +26 -0
- data/test/rails_root/app/views/posts/show.rhtml +18 -0
- data/test/rails_root/app/views/users/edit.rhtml +22 -0
- data/test/rails_root/app/views/users/index.rhtml +22 -0
- data/test/rails_root/app/views/users/new.rhtml +21 -0
- data/test/rails_root/app/views/users/show.rhtml +13 -0
- data/test/rails_root/config/boot.rb +109 -0
- data/test/rails_root/config/database.yml +4 -0
- data/test/rails_root/config/environment.rb +14 -0
- data/test/rails_root/config/environments/sqlite3.rb +0 -0
- data/test/rails_root/config/initializers/new_rails_defaults.rb +15 -0
- data/test/rails_root/config/initializers/shoulda.rb +8 -0
- data/test/rails_root/config/routes.rb +6 -0
- data/test/rails_root/db/migrate/001_create_users.rb +18 -0
- data/test/rails_root/db/migrate/002_create_posts.rb +13 -0
- data/test/rails_root/db/migrate/003_create_taggings.rb +12 -0
- data/test/rails_root/db/migrate/004_create_tags.rb +11 -0
- data/test/rails_root/db/migrate/005_create_dogs.rb +12 -0
- data/test/rails_root/db/migrate/006_create_addresses.rb +14 -0
- data/test/rails_root/db/migrate/007_create_fleas.rb +11 -0
- data/test/rails_root/db/migrate/008_create_dogs_fleas.rb +12 -0
- data/test/rails_root/db/migrate/009_create_products.rb +17 -0
- data/test/rails_root/db/migrate/010_create_friendships.rb +14 -0
- data/test/rails_root/db/schema.rb +0 -0
- data/test/rails_root/public/404.html +30 -0
- data/test/rails_root/public/422.html +30 -0
- data/test/rails_root/public/500.html +30 -0
- data/test/rails_root/script/console +3 -0
- data/test/rails_root/script/generate +3 -0
- data/test/test_helper.rb +31 -0
- data/test/unit/address_test.rb +10 -0
- data/test/unit/dog_test.rb +7 -0
- data/test/unit/flea_test.rb +6 -0
- data/test/unit/friendship_test.rb +6 -0
- data/test/unit/post_test.rb +15 -0
- data/test/unit/product_test.rb +27 -0
- data/test/unit/tag_test.rb +10 -0
- data/test/unit/tagging_test.rb +6 -0
- data/test/unit/user_test.rb +47 -0
- metadata +197 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require 'shoulda'
|
|
2
|
+
require 'shoulda/controller/helpers'
|
|
3
|
+
require 'shoulda/controller/resource_options'
|
|
4
|
+
require 'shoulda/controller/macros'
|
|
5
|
+
require 'shoulda/controller/routing'
|
|
6
|
+
|
|
7
|
+
module Test # :nodoc: all
|
|
8
|
+
module Unit
|
|
9
|
+
class TestCase
|
|
10
|
+
extend ThoughtBot::Shoulda::Controller::Macros
|
|
11
|
+
include ThoughtBot::Shoulda::Controller::Helpers
|
|
12
|
+
ThoughtBot::Shoulda::Controller::VALID_FORMATS.each do |format|
|
|
13
|
+
include "ThoughtBot::Shoulda::Controller::#{format.to_s.upcase}".constantize
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
require 'shoulda/active_record/assertions'
|
|
20
|
+
require 'shoulda/action_mailer/assertions'
|
|
21
|
+
|
|
22
|
+
module ActionController #:nodoc: all
|
|
23
|
+
module Integration
|
|
24
|
+
class Session
|
|
25
|
+
include ThoughtBot::Shoulda::Assertions
|
|
26
|
+
include ThoughtBot::Shoulda::Helpers
|
|
27
|
+
include ThoughtBot::Shoulda::ActiveRecord::Assertions
|
|
28
|
+
include ThoughtBot::Shoulda::ActionMailer::Assertions
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
require 'shoulda/private_helpers'
|
|
2
|
+
|
|
3
|
+
module ThoughtBot # :nodoc:
|
|
4
|
+
module Shoulda # :nodoc:
|
|
5
|
+
module Macros
|
|
6
|
+
# Macro that creates a test asserting a change between the return value
|
|
7
|
+
# of an expression that is run before and after the current setup block
|
|
8
|
+
# is run. This is similar to Active Support's <tt>assert_difference</tt>
|
|
9
|
+
# assertion, but supports more than just numeric values. See also
|
|
10
|
+
# should_not_change.
|
|
11
|
+
#
|
|
12
|
+
# Example:
|
|
13
|
+
#
|
|
14
|
+
# context "Creating a post"
|
|
15
|
+
# setup do
|
|
16
|
+
# Post.create
|
|
17
|
+
# end
|
|
18
|
+
#
|
|
19
|
+
# should_change "Post.count", :by => 1
|
|
20
|
+
# end
|
|
21
|
+
#
|
|
22
|
+
# As shown in this example, the <tt>:by</tt> option expects a numeric
|
|
23
|
+
# difference between the before and after values of the expression. You
|
|
24
|
+
# may also specify <tt>:from</tt> and <tt>:to</tt> options:
|
|
25
|
+
#
|
|
26
|
+
# should_change "Post.count", :from => 0, :to => 1
|
|
27
|
+
# should_change "@post.title", :from => "old", :to => "new"
|
|
28
|
+
#
|
|
29
|
+
# Combinations of <tt>:by</tt>, <tt>:from</tt>, and <tt>:to</tt> are allowed:
|
|
30
|
+
#
|
|
31
|
+
# should_change "@post.title" # => assert the value changed in some way
|
|
32
|
+
# should_change "@post.title" :from => "old" # => assert the value changed to anything other than "old"
|
|
33
|
+
# should_change "@post.title" :to => "new" # => assert the value changed from anything other than "new"
|
|
34
|
+
def should_change(expression, options = {})
|
|
35
|
+
by, from, to = get_options!([options], :by, :from, :to)
|
|
36
|
+
stmt = "change #{expression.inspect}"
|
|
37
|
+
stmt << " from #{from.inspect}" if from
|
|
38
|
+
stmt << " to #{to.inspect}" if to
|
|
39
|
+
stmt << " by #{by.inspect}" if by
|
|
40
|
+
|
|
41
|
+
expression_eval = lambda { eval(expression) }
|
|
42
|
+
before = lambda { @_before_should_change = expression_eval.bind(self).call }
|
|
43
|
+
should stmt, :before => before do
|
|
44
|
+
old_value = @_before_should_change
|
|
45
|
+
new_value = expression_eval.bind(self).call
|
|
46
|
+
assert_operator from, :===, old_value, "#{expression.inspect} did not originally match #{from.inspect}" if from
|
|
47
|
+
assert_not_equal old_value, new_value, "#{expression.inspect} did not change" unless by == 0
|
|
48
|
+
assert_operator to, :===, new_value, "#{expression.inspect} was not changed to match #{to.inspect}" if to
|
|
49
|
+
assert_equal old_value + by, new_value if by
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Macro that creates a test asserting no change between the return value
|
|
54
|
+
# of an expression that is run before and after the current setup block
|
|
55
|
+
# is run. This is the logical opposite of should_change.
|
|
56
|
+
#
|
|
57
|
+
# Example:
|
|
58
|
+
#
|
|
59
|
+
# context "Updating a post"
|
|
60
|
+
# setup do
|
|
61
|
+
# @post.update_attributes(:title => "new")
|
|
62
|
+
# end
|
|
63
|
+
#
|
|
64
|
+
# should_not_change "Post.count"
|
|
65
|
+
# end
|
|
66
|
+
def should_not_change(expression)
|
|
67
|
+
expression_eval = lambda { eval(expression) }
|
|
68
|
+
before = lambda { @_before_should_not_change = expression_eval.bind(self).call }
|
|
69
|
+
should "not change #{expression.inspect}", :before => before do
|
|
70
|
+
new_value = expression_eval.bind(self).call
|
|
71
|
+
assert_equal @_before_should_not_change, new_value, "#{expression.inspect} changed"
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
private
|
|
76
|
+
|
|
77
|
+
include ThoughtBot::Shoulda::Private
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module ThoughtBot # :nodoc:
|
|
2
|
+
module Shoulda # :nodoc:
|
|
3
|
+
module Private # :nodoc:
|
|
4
|
+
# Returns the values for the entries in the args hash who's keys are listed in the wanted array.
|
|
5
|
+
# Will raise if there are keys in the args hash that aren't listed.
|
|
6
|
+
def get_options!(args, *wanted)
|
|
7
|
+
ret = []
|
|
8
|
+
opts = (args.last.is_a?(Hash) ? args.pop : {})
|
|
9
|
+
wanted.each {|w| ret << opts.delete(w)}
|
|
10
|
+
raise ArgumentError, "Unsupported options given: #{opts.keys.join(', ')}" unless opts.keys.empty?
|
|
11
|
+
return *ret
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Returns the model class constant, as determined by the test class name.
|
|
15
|
+
#
|
|
16
|
+
# class TestUser; model_class; end => User
|
|
17
|
+
def model_class
|
|
18
|
+
self.name.gsub(/Test$/, '').constantize
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Stolen straight from ActiveSupport
|
|
2
|
+
|
|
3
|
+
class Proc #:nodoc:
|
|
4
|
+
def bind(object)
|
|
5
|
+
block, time = self, Time.now
|
|
6
|
+
(class << object; self end).class_eval do
|
|
7
|
+
method_name = "__bind_#{time.to_i}_#{time.usec}"
|
|
8
|
+
define_method(method_name, &block)
|
|
9
|
+
method = instance_method(method_name)
|
|
10
|
+
remove_method(method_name)
|
|
11
|
+
method
|
|
12
|
+
end.bind(object)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'active_support'
|
|
3
|
+
require 'shoulda'
|
|
4
|
+
|
|
5
|
+
require 'shoulda/active_record' if defined? ActiveRecord::Base
|
|
6
|
+
require 'shoulda/controller' if defined? ActionController::Base
|
|
7
|
+
require 'shoulda/action_mailer' if defined? ActionMailer::Base
|
|
8
|
+
|
|
9
|
+
if defined?(RAILS_ROOT)
|
|
10
|
+
# load in the 3rd party macros from vendorized plugins and gems
|
|
11
|
+
Dir[File.join(RAILS_ROOT, "vendor", "{plugins,gems}", "*", "shoulda_macros", "*.rb")].each do |macro_file_path|
|
|
12
|
+
require macro_file_path
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# load in the local application specific macros
|
|
16
|
+
Dir[File.join(RAILS_ROOT, "test", "shoulda_macros", "*.rb")].each do |macro_file_path|
|
|
17
|
+
require macro_file_path
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
namespace :shoulda do
|
|
2
|
+
desc "List the names of the test methods in a specification like format"
|
|
3
|
+
task :list do
|
|
4
|
+
$LOAD_PATH.unshift("test")
|
|
5
|
+
|
|
6
|
+
require 'test/unit'
|
|
7
|
+
require 'rubygems'
|
|
8
|
+
require 'active_support'
|
|
9
|
+
|
|
10
|
+
# bug in test unit. Set to true to stop from running.
|
|
11
|
+
Test::Unit.run = true
|
|
12
|
+
|
|
13
|
+
test_files = Dir.glob(File.join('test', '**', '*_test.rb'))
|
|
14
|
+
test_files.each do |file|
|
|
15
|
+
load file
|
|
16
|
+
klass = File.basename(file, '.rb').classify.constantize
|
|
17
|
+
|
|
18
|
+
puts klass.name.gsub('Test', '')
|
|
19
|
+
|
|
20
|
+
test_methods = klass.instance_methods.grep(/^test/).map {|s| s.gsub(/^test: /, '')}.sort
|
|
21
|
+
test_methods.each {|m| puts " " + m }
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
namespace :shoulda do
|
|
2
|
+
# From http://blog.internautdesign.com/2007/11/2/a-yaml_to_shoulda-rake-task
|
|
3
|
+
# David.Lowenfels@gmail.com
|
|
4
|
+
desc "Converts a YAML file (FILE=./path/to/yaml) into a Shoulda skeleton"
|
|
5
|
+
task :from_yaml do
|
|
6
|
+
require 'yaml'
|
|
7
|
+
|
|
8
|
+
def yaml_to_context(hash, indent = 0)
|
|
9
|
+
indent1 = ' ' * indent
|
|
10
|
+
indent2 = ' ' * (indent + 1)
|
|
11
|
+
hash.each_pair do |context, shoulds|
|
|
12
|
+
puts indent1 + "context \"#{context}\" do"
|
|
13
|
+
puts
|
|
14
|
+
shoulds.each do |should|
|
|
15
|
+
yaml_to_context( should, indent + 1 ) and next if should.is_a?( Hash )
|
|
16
|
+
puts indent2 + "should_eventually \"" + should.gsub(/^should +/,'') + "\" do"
|
|
17
|
+
puts indent2 + "end"
|
|
18
|
+
puts
|
|
19
|
+
end
|
|
20
|
+
puts indent1 + "end"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
puts("Please pass in a FILE argument.") and exit unless ENV['FILE']
|
|
25
|
+
|
|
26
|
+
yaml_to_context( YAML.load_file( ENV['FILE'] ) )
|
|
27
|
+
end
|
|
28
|
+
end
|
data/lib/shoulda.rb
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require 'shoulda/context'
|
|
2
|
+
require 'shoulda/proc_extensions'
|
|
3
|
+
require 'shoulda/assertions'
|
|
4
|
+
require 'shoulda/macros'
|
|
5
|
+
require 'shoulda/helpers'
|
|
6
|
+
|
|
7
|
+
module Test # :nodoc: all
|
|
8
|
+
module Unit
|
|
9
|
+
class TestCase
|
|
10
|
+
extend Thoughtbot::Shoulda
|
|
11
|
+
include ThoughtBot::Shoulda::Assertions
|
|
12
|
+
extend ThoughtBot::Shoulda::Macros
|
|
13
|
+
include ThoughtBot::Shoulda::Helpers
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
data/test/README
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
The Shoulda test suite (in particular - the tests that test shoulda)
|
|
2
|
+
|
|
3
|
+
Quick overview:
|
|
4
|
+
|
|
5
|
+
The test directory contains the following files and subdirectories:
|
|
6
|
+
|
|
7
|
+
* rails_root - contains the stripped down rails application that the tests run against. The rails root contains:
|
|
8
|
+
** the models, controllers, and views defined under app/
|
|
9
|
+
** the sqlite3.rb environment file
|
|
10
|
+
** a migration file for each model
|
|
11
|
+
** a shoulda initializer that simulates loading the plugin but without relying on vendor/plugins
|
|
12
|
+
* fixtures - contain the sample DB data for each model
|
|
13
|
+
* functional - controller tests for each of the controllers under rails_root/app
|
|
14
|
+
* unit - model tests for each of the models under rails_root/app
|
|
15
|
+
* other - tests for the shoulda contexts, should statements, and assertions
|
|
16
|
+
* test_helper.rb - responsible for initializing the test environment
|
|
17
|
+
** sets the rails_env to sqlite3
|
|
18
|
+
** sets the rails_root
|
|
19
|
+
** runs all the migrations against the in-memory sqlite3 db
|
|
20
|
+
** adds some magic to load the right fixture files
|
|
21
|
+
|
|
22
|
+
In order to add a new model (or controller) to the test suite:
|
|
23
|
+
|
|
24
|
+
* add that model to rails_root/app/models
|
|
25
|
+
* add a migration for that model
|
|
26
|
+
* add a fixture file
|
|
27
|
+
* add a test for that file under test/units
|
|
28
|
+
|
|
29
|
+
Dependencies:
|
|
30
|
+
|
|
31
|
+
* Rails gem installed in the host system
|
|
32
|
+
* A working sqlite3 installation.
|
|
33
|
+
|
|
34
|
+
If you have problems running these tests, please notify the mailing list: shoulda@googlegroups.com
|
|
35
|
+
|
|
36
|
+
- Tammer Saleh <tsaleh@thoughtbot.com>
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
|
2
|
+
require 'posts_controller'
|
|
3
|
+
|
|
4
|
+
# Re-raise errors caught by the controller.
|
|
5
|
+
class PostsController; def rescue_action(e) raise e end; end
|
|
6
|
+
|
|
7
|
+
class PostsControllerTest < Test::Unit::TestCase
|
|
8
|
+
fixtures :all
|
|
9
|
+
|
|
10
|
+
def setup
|
|
11
|
+
@controller = PostsController.new
|
|
12
|
+
@request = ActionController::TestRequest.new
|
|
13
|
+
@response = ActionController::TestResponse.new
|
|
14
|
+
@post = Post.find(:first)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# autodetects the :controller
|
|
18
|
+
should_route :get, '/posts', :action => :index
|
|
19
|
+
# explicitly specify :controller
|
|
20
|
+
should_route :post, '/posts', :controller => :posts, :action => :create
|
|
21
|
+
# non-string parameter
|
|
22
|
+
should_route :get, '/posts/1', :action => :show, :id => 1
|
|
23
|
+
# string-parameter
|
|
24
|
+
should_route :put, '/posts/1', :action => :update, :id => "1"
|
|
25
|
+
should_route :delete, '/posts/1', :action => :destroy, :id => 1
|
|
26
|
+
should_route :get, '/posts/new', :action => :new
|
|
27
|
+
|
|
28
|
+
# Test the nested routes
|
|
29
|
+
should_route :get, '/users/5/posts', :action => :index, :user_id => 5
|
|
30
|
+
should_route :post, '/users/5/posts', :action => :create, :user_id => 5
|
|
31
|
+
should_route :get, '/users/5/posts/1', :action => :show, :id => 1, :user_id => 5
|
|
32
|
+
should_route :delete, '/users/5/posts/1', :action => :destroy, :id => 1, :user_id => 5
|
|
33
|
+
should_route :get, '/users/5/posts/new', :action => :new, :user_id => 5
|
|
34
|
+
should_route :put, '/users/5/posts/1', :action => :update, :id => 1, :user_id => 5
|
|
35
|
+
|
|
36
|
+
context "The public" do
|
|
37
|
+
setup do
|
|
38
|
+
@request.session[:logged_in] = false
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
should_be_restful do |resource|
|
|
42
|
+
resource.parent = :user
|
|
43
|
+
|
|
44
|
+
resource.denied.actions = [:index, :show, :edit, :new, :create, :update, :destroy]
|
|
45
|
+
resource.denied.flash = /what/i
|
|
46
|
+
resource.denied.redirect = '"/"'
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
context "Logged in" do
|
|
51
|
+
setup do
|
|
52
|
+
@request.session[:logged_in] = true
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
should_be_restful do |resource|
|
|
56
|
+
resource.parent = :user
|
|
57
|
+
|
|
58
|
+
resource.create.params = { :title => "first post", :body => 'blah blah blah'}
|
|
59
|
+
resource.update.params = { :title => "changed" }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
context "viewing posts for a user" do
|
|
63
|
+
setup do
|
|
64
|
+
get :index, :user_id => users(:first)
|
|
65
|
+
end
|
|
66
|
+
should_respond_with :success
|
|
67
|
+
should_assign_to :user, :posts
|
|
68
|
+
should_not_assign_to :foo, :bar
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
|
2
|
+
require 'users_controller'
|
|
3
|
+
|
|
4
|
+
# Re-raise errors caught by the controller.
|
|
5
|
+
class UsersController; def rescue_action(e) raise e end; end
|
|
6
|
+
|
|
7
|
+
class UsersControllerTest < Test::Unit::TestCase
|
|
8
|
+
fixtures :all
|
|
9
|
+
|
|
10
|
+
def setup
|
|
11
|
+
@controller = UsersController.new
|
|
12
|
+
@request = ActionController::TestRequest.new
|
|
13
|
+
@response = ActionController::TestResponse.new
|
|
14
|
+
@user = User.find(:first)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
should_be_restful do |resource|
|
|
18
|
+
resource.identifier = :id
|
|
19
|
+
resource.klass = User
|
|
20
|
+
resource.object = :user
|
|
21
|
+
resource.parent = []
|
|
22
|
+
resource.actions = [:index, :show, :new, :edit, :update, :create, :destroy]
|
|
23
|
+
resource.formats = [:html, :xml]
|
|
24
|
+
|
|
25
|
+
resource.create.params = { :name => "bob", :email => 'bob@bob.com', :age => 13, :ssn => "123456789"}
|
|
26
|
+
resource.update.params = { :name => "sue" }
|
|
27
|
+
|
|
28
|
+
resource.create.redirect = "user_url(@user)"
|
|
29
|
+
resource.update.redirect = "user_url(@user)"
|
|
30
|
+
resource.destroy.redirect = "users_url"
|
|
31
|
+
|
|
32
|
+
resource.create.flash = /created/i
|
|
33
|
+
resource.update.flash = /updated/i
|
|
34
|
+
resource.destroy.flash = /removed/i
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
|
2
|
+
|
|
3
|
+
class ContextTest < Test::Unit::TestCase # :nodoc:
|
|
4
|
+
|
|
5
|
+
def self.context_macro(&blk)
|
|
6
|
+
context "with a subcontext made by a macro" do
|
|
7
|
+
setup { @context_macro = :foo }
|
|
8
|
+
|
|
9
|
+
merge_block &blk
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# def self.context_macro(&blk)
|
|
14
|
+
# context "with a subcontext made by a macro" do
|
|
15
|
+
# setup { @context_macro = :foo }
|
|
16
|
+
# yield # <- this doesn't work.
|
|
17
|
+
# end
|
|
18
|
+
# end
|
|
19
|
+
|
|
20
|
+
context "context with setup block" do
|
|
21
|
+
setup do
|
|
22
|
+
@blah = "blah"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
should "run the setup block" do
|
|
26
|
+
assert_equal "blah", @blah
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
should "have name set right" do
|
|
30
|
+
assert_match(/^test: context with setup block/, self.to_s)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
context "and a subcontext" do
|
|
34
|
+
setup do
|
|
35
|
+
@blah = "#{@blah} twice"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
should "be named correctly" do
|
|
39
|
+
assert_match(/^test: context with setup block and a subcontext should be named correctly/, self.to_s)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
should "run the setup blocks in order" do
|
|
43
|
+
assert_equal @blah, "blah twice"
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
context_macro do
|
|
48
|
+
should "have name set right" do
|
|
49
|
+
assert_match(/^test: context with setup block with a subcontext made by a macro should have name set right/, self.to_s)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
should "run the setup block of that context macro" do
|
|
53
|
+
assert_equal :foo, @context_macro
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
should "run the setup block of the main context" do
|
|
57
|
+
assert_equal "blah", @blah
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
context "another context with setup block" do
|
|
64
|
+
setup do
|
|
65
|
+
@blah = "foo"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
should "have @blah == 'foo'" do
|
|
69
|
+
assert_equal "foo", @blah
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
should "have name set right" do
|
|
73
|
+
assert_match(/^test: another context with setup block/, self.to_s)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
context "context with method definition" do
|
|
78
|
+
setup do
|
|
79
|
+
def hello; "hi"; end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
should "be able to read that method" do
|
|
83
|
+
assert_equal "hi", hello
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
should "have name set right" do
|
|
87
|
+
assert_match(/^test: context with method definition/, self.to_s)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
context "another context" do
|
|
92
|
+
should "not define @blah" do
|
|
93
|
+
assert_nil @blah
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
context "context with multiple setups and/or teardowns" do
|
|
98
|
+
|
|
99
|
+
cleanup_count = 0
|
|
100
|
+
|
|
101
|
+
2.times do |i|
|
|
102
|
+
setup { cleanup_count += 1 }
|
|
103
|
+
teardown { cleanup_count -= 1 }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
2.times do |i|
|
|
107
|
+
should "call all setups and all teardowns (check ##{i + 1})" do
|
|
108
|
+
assert_equal 2, cleanup_count
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
context "subcontexts" do
|
|
113
|
+
|
|
114
|
+
2.times do |i|
|
|
115
|
+
setup { cleanup_count += 1 }
|
|
116
|
+
teardown { cleanup_count -= 1 }
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
2.times do |i|
|
|
120
|
+
should "also call all setups and all teardowns in parent and subcontext (check ##{i + 1})" do
|
|
121
|
+
assert_equal 4, cleanup_count
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
should_eventually "pass, since it's unimplemented" do
|
|
130
|
+
flunk "what?"
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
should_eventually "not require a block when using should_eventually"
|
|
134
|
+
should "pass without a block, as that causes it to piggyback to should_eventually"
|
|
135
|
+
|
|
136
|
+
context "context for testing should piggybacking" do
|
|
137
|
+
should "call should_eventually as we are not passing a block"
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
context "context" do
|
|
141
|
+
context "with nested subcontexts" do
|
|
142
|
+
should_eventually "only print this statement once for a should_eventually"
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
|
|
3
|
+
class ConvertToShouldSyntaxTest < Test::Unit::TestCase # :nodoc:
|
|
4
|
+
|
|
5
|
+
BEFORE_FIXTURE = <<-EOS
|
|
6
|
+
class DummyTest < Test::Unit::TestCase
|
|
7
|
+
|
|
8
|
+
should "Not change this_word_with_underscores" do
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def test_should_be_working
|
|
12
|
+
assert true
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_some_cool_stuff
|
|
16
|
+
assert true
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def non_test_method
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
end
|
|
23
|
+
EOS
|
|
24
|
+
|
|
25
|
+
AFTER_FIXTURE = <<-EOS
|
|
26
|
+
class DummyTest < Test::Unit::TestCase
|
|
27
|
+
|
|
28
|
+
should "Not change this_word_with_underscores" do
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
should "be working" do
|
|
32
|
+
assert true
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
should "RENAME ME: test some cool stuff" do
|
|
36
|
+
assert true
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def non_test_method
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
end
|
|
43
|
+
EOS
|
|
44
|
+
|
|
45
|
+
FIXTURE_PATH = "./convert_to_should_syntax_fixture.dat"
|
|
46
|
+
|
|
47
|
+
RUBY = ENV['RUBY'] || 'ruby'
|
|
48
|
+
|
|
49
|
+
def test_convert_to_should_syntax
|
|
50
|
+
File.open(FIXTURE_PATH, "w") {|f| f.write(BEFORE_FIXTURE)}
|
|
51
|
+
cmd = "#{RUBY} #{File.join(File.dirname(__FILE__), '../../bin/convert_to_should_syntax')} #{FIXTURE_PATH}"
|
|
52
|
+
output = `#{cmd}`
|
|
53
|
+
File.unlink($1) if output.match(/has been stored in '([^']+)/)
|
|
54
|
+
assert_match(/has been converted/, output)
|
|
55
|
+
result = IO.read(FIXTURE_PATH)
|
|
56
|
+
assert_equal result, AFTER_FIXTURE
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def teardown
|
|
60
|
+
File.unlink(FIXTURE_PATH)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
end
|