Permalink
Browse files

allow multiple blocks to be registered for the same name by multiple …

…calls to #share
  • Loading branch information...
Sven Fuchs
Sven Fuchs committed Dec 1, 2008
1 parent cb0918f commit 79db5a2bb5642842c8423bd9a7401b742bddfd4d
Showing with 31 additions and 18 deletions.
  1. +10 −1 README.textile
  2. +2 −1 lib/with/dsl.rb
  3. +19 −16 test/demo.rb
View
@@ -73,10 +73,19 @@ them.
E.g. with a definition like:
<pre><code>
+ # share multiple contexts with the same name:
+ share :invalid_article_params do
+ before { @params = valid_article_params.except(:title) }
+ end
+
+ share :invalid_article_params do
+ before { @params = valid_article_params.except(:body) }
+ end
+
+ # or register them at once:
share :invalid_article_params,
lambda { before { @params = valid_article_params.except(:title) } },
lambda { before { @params = valid_article_params.except(:body) } }
- # TODO figure out how to reduce this syntax
</code></pre>
... it would generate two methods and test the actual assertions against both
View
@@ -43,7 +43,8 @@ module Sharing
def share(*blocks, &block)
name = blocks.shift
blocks << block if block
- shared[name] = blocks
+ shared[name] ||= []
+ shared[name] += blocks
end
def shared
View
@@ -59,11 +59,27 @@ def require_admin
const_set :Routes, set
end
-# set up some macros and share some contexts
+# share some contexts and set up some macros
class ActionController::TestCase
include With
+ share :login_as_admin do
+ before { @controller.current_user = User.new(true) }
+ end
+
+ share :valid_article_params do
+ before { @params = valid_article_params }
+ end
+
+ share :invalid_article_params do
+ before { @params = valid_article_params.except(:title) }
+ end
+
+ share :invalid_article_params do
+ before { @params = valid_article_params.except(:body) }
+ end
+
def it_redirects_to(path)
assert_redirected_to path
end
@@ -72,18 +88,9 @@ def it_assigns_flash(key, pattern)
assert flash[:error] =~ pattern
end
- share :login_as_admin do
- before { @controller.current_user = User.new(true) }
- end
-
- share :valid_article_params do
- before { @params = valid_article_params }
+ def valid_article_params
+ { :title => 'an article title', :body => 'an article body' }
end
-
- # TODO figure out how to reduce this syntax
- share :invalid_article_params,
- lambda { before { @params = valid_article_params.except(:title) } },
- lambda { before { @params = valid_article_params.except(:body) } }
end
# TODO figure out how to reduce this
@@ -96,10 +103,6 @@ def it_redirects_to(path)
# and now the fun starts ...
class ArticlesControllerTest < ActionController::TestCase
- def valid_article_params
- { :title => 'an article title', :body => 'an article body' }
- end
-
describe 'POST to :create' do
action { post :create, @params }

0 comments on commit 79db5a2

Please sign in to comment.