0

ここで、rr gem を使用して Project モデルの count メソッドをスタブ化し、index アクションをレプリケートして count メソッドが呼び出されたかどうかを確認します。mocha gem を使用する予定ですが、 mochaassert_received gem のメソッドと同等のものは何かわかりません。次のコードは、私のテストの例の 1 つです。

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
4

1 に答える 1

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 があります。

于 2015-06-23T15:56:24.777 に答える