RSpec学習

RSpecのソースを参考に、練習で書いたspec.
どちらかというと、全体のパスを通すテストだが、GUI部分はメソッドオーバライドで逃げた.
Ruby + RSpecだと it shouldの部分が、かなり簡潔にかける。
3〜4行に収めようとするフォースをRSpecに感じとれた!!

require File.dirname(__FILE__) + '/../../lib/quickmate/choice_switch_command'

module QuickMate
  describe ChoiceSwitchCommand, "file choice targets" do    
    def choice(expected)
      Pairs.new(expected)
    end
    
    before(:each) do
      @command = ChoiceSwitchCommand.new
      @common_path = "#{ENV["TM_PROJECT_DIRECTORY"]}/Support/fixtures/exist_file"
    end

    it "should choice many file when current file is lib (one to many)" do
      @command.open_pair("#{@common_path}/lib/foo/one_to_many.rb").
        should choice([
                    "#{@common_path}/spec/foo/feature_1-one_to_many_spec.rb",
                    "#{@common_path}/spec/foo/feature_2-one_to_many_spec.rb",
                    "#{@common_path}/spec/foo/one_to_many_spec.rb"])
    end

    it "should choice one file when current file is spec (many to one)" do
      @command.open_pair("#{@common_path}/spec/foo/feature_1-one_to_many_spec.rb").
        should choice(["#{@common_path}/lib/foo/one_to_many.rb"])
    end

    it "should choice one file when current file is lib (one to one)" do
      @command.open_pair("#{@common_path}/lib/foo/one_to_one.rb").
        should choice(["#{@common_path}/spec/foo/one_to_one_spec.rb"])
    end
  end
  
  class ChoiceSwitchCommand
    # hook mock
    def open_menu(path_line_views)
      # collect paths
      path_line_views.collect { |path_line_views| 
        path_line_views[0]
      }
    end
  end
  
  class Pairs
    def initialize(expected)
      @expected = expected
    end

    def matches?(actual)
      @actual = actual
      @actual.should == @expected
      true
    end

    def failure_message
      "expected #{@actual.inspect} to choice #{@expected.inspect}, but it didn't"
    end

    def negative_failure_message
      "expected #{@actual.inspect} not to choice #{@expected.inspect}, but it did"
    end
    
    def description
      "choice #{@actual} <=> #{@expected}"
    end
  end  
end

英語、勉強したほうがいいなぁ.