RailsCasts中文版,#12 Refactoring User Name Part 3 重构实战3

重构实战2重构实战1里,我们讨论了与重构、测试有关的话题。截止上一篇结束时,完成了对User类的重构,但是测试用例代码还有些乱。让我们看看如何清理一下。

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的结果。

def full_name(first, middle, last)

  User.new(:first_name => first, :middle_initial => middle, :last_name => last).full_name

end

UserTest类中的辅助方法。

这样一来,测试用例代码可以被精简为下面的样子:

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

简化后的测试代码。

运行之后,所有的用例依然能够通过。

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方法中多传入一个字符串,测试不通过时会打印出来以便定位。

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代码

这三篇文章,我们对代码和测试用例都分别进行了重构,代码的可读性和可维护性都得到了提升。好啦!希望这个小例子能让你感受到进行重构和使用测试用例的益处吧!

 

作者授权:Your welcome to post the translated text on your blog as well if the episode is free(not Pro). I just ask that you post a link back to the original episode on railscasts.com.

原文链接:http://railscasts.com/episodes/12-refactoring-user-name-part-3

你可能感兴趣的:(refactor)