Reputation: 120
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 console 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?
Upvotes: 0
Views: 1609
Reputation: 136
As said on Twitter: You're setting you your mock after you're doing your request :-)
Upvotes: 4