title: Mocha not mocking a class method in a functional test (Rails 3)
body: <p>In a rails 3 app, I'm using mocha to do some mocking in my functional tests. However it doesn't seem to  mock a class method in the functional controller.</p>
  
  <p>Controller code</p>
  
  <pre><code>class TagsController &lt; ApplicationController
    respond_to :json
  
    def index
      response = User.tags_starting_with(params[:query])
      respond_with response
    end
  end
  </code></pre>
  
  <p>Functional test</p>
  
  <pre><code>class TagsControllerTest &lt; ActionController::TestCase
    context "index action with query" do
      setup do
        query = "A_QUERY"
         get :index, :query =&gt; query, :format =&gt; "json"
         @tags = ["these", "are", "test", "tags"]
         User.expects(:tags_starting_with).returns(@tags).once
       end
  
      should "return JSON formatted tags array" do
        tags = JSON::parse @response.body
        assert_equal @tags, tags
      end
    end
  end
  </code></pre>
  
  <p>Gemfile</p>
  
  <pre><code>gem "mocha"
  </code></pre>
  
  <p>If I run this test, I keep running into</p>
  
  <pre><code>- expected exactly once, not yet invoked: User.tags_starting_with(any_parameters)
  </code></pre>
  
  <p>If I use <code>rails console test</code> I can mock a class method just fine, and it works as expected.</p>
  
  <p>I've been through <a href="https://stackoverflow.com/questions/3118866/mocha-mock-carries-to-another-test">this post</a> and have done the Gemfile, <code>require "false"</code> bit. But to no avail, it just doesn't want to mock the class method of the <code>User</code> in the controller.</p>
  
  <p>Other things I've tried, if I do <code>User.tags_starting_with("bla")</code> in the test itself, the expectation passes.</p>
  
  <p>So any ideas on why the <code>User</code> in the controller isn't being mocked correctly?</p>