2011-05-13 63 views
0

在rails 3应用程序中,我使用摩卡在我的功能测试中做了一些嘲弄。然而,它似乎并没有模拟功能控制器中的类方法。摩卡没有嘲笑功能测试中的类方法(Rails 3)

控制器代码

class TagsController < ApplicationController 
    respond_to :json 

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

功能测试

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" 

如果我运行这个测试,我一直运行到

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

如果我使用rails console test,我可以嘲笑一个类的方法就好了,它按预期工作。

我已经通过this post并已完成Gemfile,require "false"位。但无济于事,它只是不想嘲笑控制器中User的分类方法。

我试过的其他事情,如果我在测试本身做User.tags_starting_with("bla"),期望就会过去。

因此,有关控制器中User为什么没有被正确模拟的任何想法?

回答

3

由于在Twitter上说: 你设置你的模拟你做你的要求:-)

+0

之后还有我在嘲笑关于与负载路径的Gemfile杂耍。谢谢! – pjaspers 2011-05-13 09:32:31