title: How to unit test a class that depends heavily on other classes?
body: <p>My understanding is that unit testing should test classes in isolation, focusing on granular behavior and substituting objects of other classes using doubles/mocks wherever possible. (Please correct me if I'm wrong here.)</p>
  
  <p>I'm writing a gem with a class called <code>MatchList</code>. <code>MatchList::new</code> takes two arguments, each an instance of another class called <code>MatchPhrase</code>. <code>MatchPhrase</code> contains some behavior that <code>MatchList</code> depends heavily on (<em>i.e.,</em> if you feed anything other than a <code>MatchPhrase</code> to <code>MatchList::new</code>, you're going to get a bunch of "undefined method" errors). </p>
  
  <p>My current (naive?) test setup uses <code>let</code> statements to assign variables for use in my examples:</p>
  
  <pre class="lang-rb prettyprint-override"><code>let(:query)      { MatchPhrase.new('Good Eats') }
  let(:candidate)  { MatchPhrase.new('Good Grief') }
  let(:match_list) { MatchList.new(query, candidate) }
  </code></pre>
  
  <p>How do I write this unit test? Am I right in thinking it should be done without invoking the <code>MatchPhrase</code> class? Is that even possible?</p>
  
  <hr>
  
  <p>For reference, here is what the <code>MatchList</code> class looks like:</p>
  
  <pre class="lang-rb prettyprint-override"><code>class MatchList &lt; Array
    attr_reader :query, :this_phrase, :that_phrase
  
    def initialize(query, candidate)
      super(query.length)
      @query = query
      @this_phrase = query.dup
      @that_phrase = candidate
      find_matches until none?(&amp;:nil?)
    end
  
    private
  
    def find_matches
      query.each.with_index do |this_token, i|
        next unless self[i].nil?
        that_token = this_token.best_match_in(that_phrase)
        next if that_token.match?(that_token) &amp;&amp;
                this_token != that_token.best_match_in(this_phrase)
        self[i] = this_token.match?(that_token) ? that_token : NilToken.new
        this_phrase.delete_once(this_token)
        that_phrase.delete_once(that_token)
      end
    end
  end
  </code></pre>