Skip to content
This repository
Browse code

Test that #[] and #[]= keep working when #read_attribute and #write_a…

…ttribute are overridden
  • Loading branch information...
commit cda5094abd605d8612b4b031cf870a2004938125 1 parent f22c36b
Tom Stuart tomstuart authored

Showing 1 changed file with 33 additions and 0 deletions. Show diff stats Hide diff stats

  1. +33 0 activerecord/test/cases/attribute_methods_test.rb
33 activerecord/test/cases/attribute_methods_test.rb
@@ -294,6 +294,39 @@ def test_read_write_boolean_attribute
294 294 # puts ""
295 295 end
296 296
  297 + def test_overridden_write_attribute
  298 + topic = Topic.new
  299 + def topic.write_attribute(attr_name, value)
  300 + super(attr_name, value.downcase)
  301 + end
  302 +
  303 + topic.send(:write_attribute, :title, "Yet another topic")
  304 + assert_equal "yet another topic", topic.title
  305 +
  306 + topic[:title] = "Yet another topic: part 2"
  307 + assert_equal "yet another topic: part 2", topic.title
  308 +
  309 + topic.send(:write_attribute, "title", "Yet another topic: part 3")
  310 + assert_equal "yet another topic: part 3", topic.title
  311 +
  312 + topic["title"] = "Yet another topic: part 4"
  313 + assert_equal "yet another topic: part 4", topic.title
  314 + end
  315 +
  316 + def test_overridden_read_attribute
  317 + topic = Topic.new
  318 + topic.title = "Stop changing the topic"
  319 + def topic.read_attribute(attr_name)
  320 + super(attr_name).upcase
  321 + end
  322 +
  323 + assert_equal "STOP CHANGING THE TOPIC", topic.send(:read_attribute, "title")
  324 + assert_equal "STOP CHANGING THE TOPIC", topic["title"]
  325 +
  326 + assert_equal "STOP CHANGING THE TOPIC", topic.send(:read_attribute, :title)
  327 + assert_equal "STOP CHANGING THE TOPIC", topic[:title]
  328 + end
  329 +
297 330 def test_read_overridden_attribute
298 331 topic = Topic.new(:title => 'a')
299 332 def topic.title() 'b' end

0 comments on commit cda5094

Please sign in to comment.
Something went wrong with that request. Please try again.