rr gem在mocha gem中的assert_received等价物

时间:2015-06-23 15:13:58

标签: ruby-on-rails ruby testing mocha rr

现在我使用 rr gem来存根项目模型计数方法,然后我复制索引操作来检查是否调用了count方法。我打算使用mocha gem,但我不知道 mocha gem中assert_received方法的等价物。以下代码是我测试的一个例子。

require 'test_helper'

class ProjectsControllerTest < ActionController::TestCase
  context "on GET to index" do
    setup do
      stub(Project).count { 30000 }
      get :index
    end

    should "load up the number of gems, users, and downloads" do
       assert_received(Project)     { |subject| subject.count }
    end
  end
end

1 个答案:

答案 0 :(得分:1)

require 'test_helper'

class ProjectsControllerTest < ActionController::TestCase
  context "on GET to index" do
    setup do
      Project.stubs(:count).returns(30000)
    end

    should "load up the number of gems, users, and downloads" do
      Project.expects(:count).returns(30000)
      get :index
    end
  end
end

希望这会有所帮助,这是mocha API。