Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

moving stuff around

  • Loading branch information...
commit 5f0b1059c6fe0da233675253a8fab5328c2e7a1e 1 parent 48b45d0
@svenfuchs authored
Showing with 200 additions and 0 deletions.
  1. +137 −0 demo.rb
  2. +63 −0 test/context_test.rb
View
137 demo.rb
@@ -0,0 +1,137 @@
+require File.dirname(__FILE__) + '/test/test_helper'
+
+require 'rubygems'
+require 'actionpack'
+require 'action_controller'
+require 'action_controller/test_process'
+require 'active_support'
+
+# setup some fakes so the demo can run
+
+class Article
+ attr_reader :errors
+
+ def initialize(attributes)
+ @attributes = attributes
+ end
+
+ def save
+ @errors = ['title', 'body'] - @attributes.keys
+ @errors.empty?
+ end
+end
+
+class User
+ def initialize(admin = false)
+ @admin = admin
+ end
+
+ def admin?
+ @admin
+ end
+end
+
+class ArticlesController < ActionController::Base
+ attr_accessor :current_user
+ before_filter :require_admin
+
+ def create
+ @article = Article.new params
+ if @article.save
+ redirect_to '/articles/1'
+ else
+ flash[:error] = "missing: #{@article.errors.join(', ')}"
+ render :text => "can't fake a real template easily?"
+ end
+ end
+
+ protected
+
+ def require_admin
+ redirect_to '/login' unless current_user && current_user.admin?
+ end
+end
+
+ActionController::Routing.module_eval do
+ set = ActionController::Routing::RouteSet.new
+ set.draw {|map| map.articles 'articles', :controller => 'articles', :action => 'create'}
+ remove_const :Routes
+ const_set :Routes, set
+end
+
+# 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 = nil, &block)
+ path = instance_eval(&block) if block
+ assert_redirected_to path
+ end
+
+ def it_assigns(name)
+ assert_not_nil @controller.instance_variable_get(:"@#{name}")
+ end
+
+ def it_assigns_flash(key, pattern)
+ assert flash[:error] =~ pattern
+ end
+
+ def valid_article_params
+ { :title => 'an article title', :body => 'an article body' }
+ end
+end
+
+# and now the fun starts ...
+
+class ArticlesControllerTest < ActionController::TestCase
+ describe 'POST to :create' do
+ before do
+ # set up some preconditions
+ @artificial_precondition = true
+ end
+
+ action { post :create, @params }
+
+ with :login_as_admin do
+ it_assigns :article
+
+ it "succeeds", :with => :valid_article_params do
+ it_redirects_to { 'articles/1' }
+ end
+
+ it "fails", :with => :invalid_article_params do
+ it_assigns_flash :error, /missing: (body|title)/
+ end
+ end
+
+ with :login_as_user, :no_login do
+ it_redirects_to { '/login' }
+ end
+
+ it "has called the before block" do
+ # TODO remove this extra block
+ assertion do
+ assert @artificial_precondition
+ end
+ end
+ end
+
+ puts "tests defined: \n " + instance_methods.grep(/^test_/).join(", \n ")
+end
View
63 test/context_test.rb
@@ -0,0 +1,63 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class GroupExpandedTest < Test::Unit::TestCase
+ include GroupSetup
+
+ def setup
+ @group = setup_example_group
+ @expanded = @group.send :expand
+ end
+
+ def test_expanded_structure
+ # there's one root
+ assert_equal 1, @expanded.size
+ @root = @expanded[0]
+ assert_equal 'root', @root.name
+
+ # root has one child: context
+ assert_equal 1, @root.children.size
+ @context = @root.children[0]
+ assert_equal :context, @context.name
+
+ # context has three children: does_something and 2x nested_context
+ assert_equal 3, @context.children.size
+ @does_something = @context.children[0]
+ @nested_context_1 = @context.children[1]
+ @nested_context_2 = @context.children[1]
+
+ assert_equal 'does something', @does_something.name
+ assert_equal :nested_context, @nested_context_1.name
+ assert_equal :nested_context, @nested_context_2.name
+
+ # does_something has one child: does_something_nested
+ assert_equal 1, @does_something.children.size
+ @does_something_nested = @does_something.children[0]
+ assert_equal 'does something nested', @does_something_nested.name
+
+ # nested_context_1 and nested_context_2 have each one child: does_something_in_nested_context
+ assert_equal 1, @nested_context_1.children.size
+ assert_equal 1, @nested_context_2.children.size
+ @does_something_in_nested_context = @nested_context_1.children[0]
+
+ assert_equal 'does something in nested_context', @does_something_in_nested_context.name
+
+ # it's actually the same object instance. not sure if that could cause any trouble:
+ assert_equal @nested_context_1.children[0], @nested_context_2.children[0]
+ end
+
+ def test_expanded_leafs
+ leafs = @expanded.first.leafs
+
+ expected = [ "does something nested",
+ "does something in nested_context",
+ "does something in nested_context" ]
+ assert_equal expected, leafs.map(&:name)
+
+ expected = [ ["root", :context, "does something", "does something nested"],
+ ["root", :context, :nested_context, "does something in nested_context"],
+ ["root", :context, :nested_context, "does something in nested_context"] ]
+ result = leafs.map {|leaf| leaf.parents.map(&:name) << leaf.name }
+ assert_equal expected, result
+ end
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.