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
23 25  
11 class User < ActiveRecord::Base
22   
3   # Use mocked attributes so we don't have to hit the database
4   attr_accessor :favorite_books
5   attr_accessor :bio
6   
7   
8   templated_attribute :bio, :label => 'Tell us about yourself.'
9   
310 end
22 25  
11 RAILS_ENV = 'test'
2 
3 # Load full environment
24 require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
5 
6 
7 # Returns true if the string +input+ contains certain tags, whose innerHTML
8 # each equals a certain value (allowing for surrouding whitespace). The tags
9 # and values are specified in +tag_hash+ (format: <tt>{:tagname1 => 'inner
10 # content', :tagname2 => 'inner content 2'}</tt>).
11 #
12 # Usage:
13 #   assert_tag_contents {:textarea => 'innerHTML'}, '<textarea id="foo"> innerHTML </textarea>'  # => true
14 def assert_tag_innerHTML(tag_hash, input)
15   tag_hash.each_pair do |tag, innerHTML|
16     tag = tag.to_s
17     message = build_message '', "? tag with innerHTML containing ? is not present in input:\n?", tag, innerHTML, input
18     assert_block message do
19       input =~ /<#{tag}.*?>\s*?#{innerHTML}\s*?<\/#{tag}>/m
20     end
21   end
22 end
23 
24 
25 # Returns true if the string +input+ contains certain tags, whose innerHTML
26 # each contains a certain value. The tags and values are specified in +tag_hash+
27 # (format: <tt>{:tagname1 => 'inner content', :tagname2 => 'inner content 2'}</tt>).
28 #
29 # Usage:
30 #   assert_tag_contains {:textarea => 'innerHTML'}, '<textarea id="foo"> This is innerHTML </textarea>'  # => true
31 def assert_tag_contains(tag_hash, input)
32   tag_hash.each_pair do |tag, innerHTML|
33     tag = tag.to_s
34     message = build_message '', "? tag with innerHTML containing ? is not present in input:\n?", tag, innerHTML, input
35     assert_block message do
36       input =~ /<#{tag}.*?>.*?#{innerHTML}.*?<\/#{tag}>/m
37     end
38   end
39 end