1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Reuse already existing classes for ActiveModel tests.

This commit is contained in:
José Valim 2010-07-21 13:00:56 +02:00
parent 508fba9e07
commit 53310614d7
4 changed files with 44 additions and 63 deletions

View file

@ -1,63 +0,0 @@
require 'cases/helper'
class Comment
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_reader :id
def to_key; id ? [id] : nil end
def save; @id = 1 end
def new_record?; @id.nil? end
def name
@id.nil? ? 'new comment' : "comment ##{@id}"
end
end
class Sheep
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_reader :id
def to_key; id ? [id] : nil end
def save; @id = 1 end
def new_record?; @id.nil? end
def name
@id.nil? ? 'new sheep' : "sheep ##{@id}"
end
end
class NamingHelpersTest < Test::Unit::TestCase
def setup
@klass = Comment
@record = @klass.new
@singular = 'comment'
@plural = 'comments'
@uncountable = Sheep
end
def test_singular
assert_equal @singular, singular(@record)
end
def test_singular_for_class
assert_equal @singular, singular(@klass)
end
def test_plural
assert_equal @plural, plural(@record)
end
def test_plural_for_class
assert_equal @plural, plural(@klass)
end
def test_uncountable
assert_equal true, uncountable?(@uncountable)
assert_equal false, uncountable?(@klass)
end
private
def method_missing(method, *args)
ActiveModel::Naming.send(method, *args)
end
end

View file

@ -1,4 +1,6 @@
require 'cases/helper'
require 'models/contact'
require 'models/sheep'
require 'models/track_back'
class NamingTest < ActiveModel::TestCase
@ -26,3 +28,40 @@ class NamingTest < ActiveModel::TestCase
assert_equal 'post/track_backs/track_back', @model_name.partial_path
end
end
class NamingHelpersTest < Test::Unit::TestCase
def setup
@klass = Contact
@record = @klass.new
@singular = 'contact'
@plural = 'contacts'
@uncountable = Sheep
end
def test_singular
assert_equal @singular, singular(@record)
end
def test_singular_for_class
assert_equal @singular, singular(@klass)
end
def test_plural
assert_equal @plural, plural(@record)
end
def test_plural_for_class
assert_equal @plural, plural(@klass)
end
def test_uncountable
assert uncountable?(@uncountable), "Expected 'sheep' to be uncoutable"
assert !uncountable?(@klass), "Expected 'contact' to be countable"
end
private
def method_missing(method, *args)
ActiveModel::Naming.send(method, *args)
end
end

View file

@ -1,4 +1,5 @@
class Contact
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_accessor :id, :name, :age, :created_at, :awesome, :preferences

View file

@ -0,0 +1,4 @@
class Sheep
extend ActiveModel::Naming
end