Commit 54a29b33aa3119f4f687a144ab44214f6d6a1431

Authored by Antonio Terceiro
1 parent 0c8eff93

rails3: fix RecentDocumentsBlock unit tests

This involved a small refactoring on how the test interacts with the
object under test and on how the rendering of the block contents is
done.
app/models/recent_documents_block.rb
@@ -14,20 +14,26 @@ class RecentDocumentsBlock < Block @@ -14,20 +14,26 @@ class RecentDocumentsBlock < Block
14 14
15 settings_items :limit, :type => :integer, :default => 5 15 settings_items :limit, :type => :integer, :default => 5
16 16
17 - include Rails.application.routes.url_helpers  
18 def content(args={}) 17 def content(args={})
19 - docs = self.limit.nil? ? owner.recent_documents(nil, {}, false) : owner.recent_documents(self.limit, {}, false)  
20 - block_title(title) +  
21 - content_tag('ul', docs.map {|item| content_tag('li', link_to(h(item.title), item.url))}.join("\n")) 18 + docs = self.docs
  19 + title = self.title
  20 + proc do
  21 + block_title(title) +
  22 + content_tag('ul', docs.map {|item| content_tag('li', link_to(h(item.title), item.url))}.join("\n"))
  23 + end
22 end 24 end
23 25
24 def footer 26 def footer
25 return nil unless self.owner.is_a?(Profile) 27 return nil unless self.owner.is_a?(Profile)
26 28
27 profile = self.owner 29 profile = self.owner
28 - lambda do 30 + proc do
29 link_to _('All content'), :profile => profile.identifier, :controller => 'profile', :action => 'sitemap' 31 link_to _('All content'), :profile => profile.identifier, :controller => 'profile', :action => 'sitemap'
30 end 32 end
31 end 33 end
32 34
  35 + def docs
  36 + self.limit.nil? ? owner.recent_documents(nil, {}, false) : owner.recent_documents(self.limit, {}, false)
  37 + end
  38 +
33 end 39 end
test/unit/recent_documents_block_test.rb
@@ -3,16 +3,24 @@ require File.dirname(__FILE__) + '/../test_helper' @@ -3,16 +3,24 @@ require File.dirname(__FILE__) + '/../test_helper'
3 class RecentDocumentsBlockTest < ActiveSupport::TestCase 3 class RecentDocumentsBlockTest < ActiveSupport::TestCase
4 4
5 def setup 5 def setup
  6 + @articles = []
6 @profile = create_user('testinguser').person 7 @profile = create_user('testinguser').person
7 ['first', 'second', 'third', 'fourth', 'fifth'].each do |name| 8 ['first', 'second', 'third', 'fourth', 'fifth'].each do |name|
8 - @profile.articles << TextArticle.create(:name => name) 9 + article = @profile.articles.create!(:name => name)
  10 + @articles << article
9 end 11 end
10 12
11 - box = Box.create!(:owner => profile)  
12 - @block = RecentDocumentsBlock.create!(:box_id => box.id) 13 + box = Box.new
  14 + box.owner = profile
  15 + box.save!
  16 +
  17 +
  18 + @block = RecentDocumentsBlock.new
  19 + @block.box_id = box.id
  20 + @block.save!
13 21
14 end 22 end
15 - attr_reader :block, :profile 23 + attr_reader :block, :profile, :articles
16 24
17 should 'describe itself' do 25 should 'describe itself' do
18 assert_not_equal Block.description, RecentDocumentsBlock.description 26 assert_not_equal Block.description, RecentDocumentsBlock.description
@@ -22,26 +30,31 @@ class RecentDocumentsBlockTest &lt; ActiveSupport::TestCase @@ -22,26 +30,31 @@ class RecentDocumentsBlockTest &lt; ActiveSupport::TestCase
22 assert_not_equal Block.new.default_title, RecentDocumentsBlock.new.default_title 30 assert_not_equal Block.new.default_title, RecentDocumentsBlock.new.default_title
23 end 31 end
24 32
25 - should 'output list with links to recent documents' do  
26 - output = block.content  
27 -  
28 - assert_match /href=.*\/testinguser\/first/, output  
29 - assert_match /href=.*\/testinguser\/second/, output  
30 - assert_match /href=.*\/testinguser\/third/, output  
31 - assert_match /href=.*\/testinguser\/fourth/, output  
32 - assert_match /href=.*\/testinguser\/fifth/, output 33 + should 'list recent documents' do
  34 + assert_equivalent block.docs, articles
  35 + end
  36 +
  37 + should 'link to documents' do
  38 + articles.each do |a|
  39 + expects(:link_to).with(a.title, a.url)
  40 + end
  41 + stubs(:block_title).returns("")
  42 + stubs(:content_tag).returns("")
  43 + stubs(:li).returns("")
  44 +
  45 + instance_eval(&block.content)
33 end 46 end
34 47
35 should 'respect the maximum number of items as configured' do 48 should 'respect the maximum number of items as configured' do
36 block.limit = 3 49 block.limit = 3
37 50
38 - output = block.content 51 + list = block.docs
39 52
40 - assert_match /href=.*\/testinguser\/fifth/, output  
41 - assert_match /href=.*\/testinguser\/fourth/, output  
42 - assert_match /href=.*\/testinguser\/third/, output  
43 - assert_no_match /href=.*\/testinguser\/second/, output  
44 - assert_no_match /href=.*\/testinguser\/first/, output 53 + assert_includes list, articles[4]
  54 + assert_includes list, articles[3]
  55 + assert_includes list, articles[2]
  56 + assert_not_includes list, articles[1]
  57 + assert_not_includes list, articles[0]
45 end 58 end
46 59
47 should 'store limit as a number' do 60 should 'store limit as a number' do
@@ -70,12 +83,9 @@ class RecentDocumentsBlockTest &lt; ActiveSupport::TestCase @@ -70,12 +83,9 @@ class RecentDocumentsBlockTest &lt; ActiveSupport::TestCase
70 end 83 end
71 84
72 should 'be able to update display setting' do 85 should 'be able to update display setting' do
73 - user = create_user('testinguser').person  
74 - box = fast_create(Box, :owner_id => user.id)  
75 - block = RecentDocumentsBlock.create!(:display => 'never', :box => box)  
76 - assert block.update_attributes!(:display => 'always')  
77 - block.reload  
78 - assert_equal 'always', block.display 86 + assert @block.update_attributes!(:display => 'always')
  87 + @block.reload
  88 + assert_equal 'always', @block.display
79 end 89 end
80 90
81 end 91 end