#12 Refactoring User Name Part 3
Mar 30, 2007 | 4 minutes | Refactoring, Testing
In the final part of this series you will see how to refactor your tests. Keeping tests clean is important because it will make testing easier to do in the future.
在上一篇和更前一篇里,我们讨论了与重构、测试有关的话题。截止上一篇结束时,完成了对User
类的重构,但是测试用例代码还有些乱。让我们看看如何清理一下。
ruby
require 'test_helper' class UserTest < ActiveSupport::TestCase test "full name without middle initial" do user = User.new(:first_name => "John", :last_name => "Smith") assert_equal 'John Smith', user.full_name end test "full name with middle initial" do user = User.new(:first_name => "Paul", :middle_initial => "P", :last_name => "Hughes") assert_equal 'Paul P. Hughes', user.full_name end test "full name with empty middle initial" do user = User.new(:first_name => "John", :middle_initial => "", :last_name => "Jones") assert_equal 'John Jones', user.full_name end end
User
类的测试用例代码。
现在这三个用例,有大量的冗余代码。都是分别创建一个User
的对象,然后比较full_name
方法的返回值与我们的期待值是否一致。应该创建一个方法,帮我们完成创建对象及返回full_name
的结果。
ruby
def full_name(first, middle, last) User.new(:first_name => first, :middle_initial => middle, :last_name => last).full_name end
UserTest
类中的辅助方法。
这样一来,测试用例代码可以被精简为下面的样子:
ruby
test "full name without middle initial" do assert_equal "John Smith", full_name('John', nil, 'Smith') end test "full name with middle initial" do assert_equal 'Paul P. Hughes', full_name('Paul', 'P', 'Hughes') end test "full name with empty middle initial" do assert_equal "John Jones", full_name('John', '', 'Jones') end
简化后的测试代码。
运行之后,所有的用例依然能够通过。
terminal
Laa-Laa:ep11 eifion$ autotest loading autotest/rails /opt/local/bin/ruby -I.:lib:test -rtest/unit -e "%w[test/unit/user_test.rb test/functional/users_controller_test.rb].each { |f| require f }" | unit_diff -u Loaded suite -e Started ... Finished in 0.282538 seconds. 3 tests, 3 assertions, 0 failures, 0 errors
输出显示用例执行通过。
测试代码变得简单多了,可以把他们移动到一个用例中。但这么做同时带来了一个新的问题,如果某一个测试条件没有满足,怎样识别出来呢?可以再assert_equal
方法中多传入一个字符串,测试不通过时会打印出来以便定位。
ruby
require 'test_helper' class UserTest < ActiveSupport::TestCase test "full name" do assert_equal "John Smith", full_name('John', nil, 'Smith'), 'nil middle initial' assert_equal 'Paul P. Hughes', full_name('Paul', 'P', 'Hughes'), 'P middle initial' assert_equal "John Jones", full_name('John', '', 'Jones'), 'blank middle initial' end def full_name(first, middle, last) User.new(:first_name => first, :middle_initial => middle, :last_name => last).full_name end end
重构后的UserTest
代码
这三篇文章,我们对代码和测试用例都分别进行了重构,代码的可读性和可维护性都得到了提升。好啦!希望这个小例子能让你感受到进行重构和使用测试用例的益处吧!