开发者

Mocha not mocking a class method in a functional test (Rails 3)

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.

Controller code

class TagsController < ApplicationController
  respond_to :json

  def index
    response = User.tags_starting_with(params[:query])
    respond_with response
  end
end

Functional test

class TagsControllerTest < ActionController::TestCase
  context "index action with query" do
    setup do
      query = "A_QUERY"
       get :index, :query => query, :format => "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

Gemfile

gem "mocha"

If I run this test, I keep running into

- expected exactly once, not yet invoked: User.tags_starting_with(any_parameters)

If I use rails consol开发者_Python百科e test I can mock a class method just fine, and it works as expected.

I've been through this post and have done the Gemfile, require "false" bit. But to no avail, it just doesn't want to mock the class method of the User in the controller.

Other things I've tried, if I do User.tags_starting_with("bla") in the test itself, the expectation passes.

So any ideas on why the User in the controller isn't being mocked correctly?


As said on Twitter: You're setting you your mock after you're doing your request :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜