avatar

25

* Put attributes in fixture to be more DRY * Add assert_tag_innerHTML and assert_tag_contains to do lightweight regex-based output checking * Add testing framework and beginnings of tests for FormHelper additions by chrisk, 29 Jul, 2007 12:46 AM
Diff this changeset:
templated_attribute_helper_test.rb
      require 'test_helper'
require 'action_controller/test_process'
require File.dirname(__FILE__) + '/fixtures/user'


# Simple RESTful routes for UsersController below
ActionController::Routing::Routes.draw do |map|
  map.resources :users
end


# Simple controller to return different Erb templates; hijack 'new' to display templates
# for an unsaved record, and 'edit' to display templates for records that already have
# saved data.
class UsersController < ActionController::Base

  # re-raise exceptions so they bubble up
  def rescue_action(e) raise e end;
    
  # for testing templated attributes on new records (where all fields have nil values)
  def new
    @user = User.new
    render :inline => case params[:attribute]
      when :bio            then '<% form_for @user do |f| %>  <%= f.text_area :bio %>  <% end %>'
      when :bio_no_js      then '<% form_for @user do |f| %>  <%= f.text_area :bio, :templated_javascript => false %>  <% end %>'
      when :favorite_books then '<% form_for @user do |f| %>  <%= f.text_area :favorite_books %>  <% end %>'
      else                      ''
    end
  end
  
  # TODO: for testing templated attributes on existing records (where fields have non-nil values)
  def edit
  end
  
end





class TemplatedAttributeHelperTest < Test::Unit::TestCase

  def setup
    @controller = UsersController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
  end
  

  def test_text_area_with_templated_attribute_should_include_template_value
    get :new, :attribute => :bio
    assert_tag_innerHTML({:textarea => 'Tell us about yourself.'}, @response.body)
  end
  
  def test_text_area_with_templated_attribute_should_include_templated_js
    get :new, :attribute => :bio
    assert_tag_contains({:script => 'new TemplatedAttribute', :script => "'label', 'Tell us about yourself.'"}, @response.body)
  end

  def test_text_area_with_templated_attribute_with_js_disabled_should_not_include_templated_js
    get :new, :attribute => :bio_no_js
    assert !@response.body.include?('</script>')
  end

  def test_text_area_without_templated_attribute_should_have_no_value_when_nil
    get :new, :attribute => :favorite_books
    assert_tag_innerHTML({:textarea => ''}, @response.body)
  end

  def test_text_area_without_templated_attribute_should_not_include_templated_js
    get :new, :attribute => :favorite_books
    assert !@response.body.include?('</script>')
  end
  
  # TODO: lots more tests to write here
  
end