2008-04-09
#012_重构用户名 PART3
关键字: rails 重构require File.dirname(__FILE__) + '/../test_helper'
class UserTest < Test::Unit::TestCase
fixtures :users
def test_full_name_without_middle_initial
user = User.new(:first_name => 'John', :last_name => 'Doe')
assert_equal 'John Doe', user.full_name
end
def test_full_name_with_middle_initial
user = User.new(:first_name => 'John', :middle_initial => 'H', :last_name => 'Doe')
assert_equal 'John H. Doe', user.full_name
end
def test_full_name_with_blank_middle_initial
user = User.new(:first_name => 'John', :middle_initial => '', :last_name => 'Doe')
assert_equal 'John Doe', user.full_name
end
end
这是011中的单元测试代码,是不是很复杂冗长,好吧,让我们来重构一下
# user_test.rb
def test_full_name
assert_equal 'John Doe', full_name('John', nil, 'Doe'), "nil middle initial"
assert_equal 'John H. Doe', full_name('John', 'H', 'Doe'), "H middle initial"
assert_equal 'John Doe', full_name('John', '', 'Doe'), "blank middle initial"
end
def full_name(first, middle, last)
User.new(:first_name => first, :middle_initial => middle, :last_name => last).full_name
end
代码是清晰多了,但有人提出来testing one assertion per test
详见:http://blog.jayfields.com/2007/06/testing-one-assertion-per-test.html
- 14:56
- 浏览 (82)
- 评论 (0)
- 分类: Railscasts学习笔记
- 相关推荐
发表评论
- 浏览: 4743 次
- 性别:

- 来自: 南京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
render与redirect_to的区 ...
redirect_to会执行被渲染的那个action,而render不会,这跟你 ...
-- by alanyuqiang -
#008_layouts和content_fo ...
这是最普遍的用法,不知道楼主有没研究过,content_for,yield在不同 ...
-- by alanyuqiang -
#008_layouts和content_fo ...
...
-- by alanyuqiang -
render与redirect_to的区 ...
不错,最近正为这个发愁呢!
-- by evil850209 -
如何在update一条记录的部 ...
theone 写道wiisola 写道theone 写道update_all [ ...
-- by wiisola






评论排行榜