现在我使用 rr gem 来 stub 项目模型的计数方法,然后我复制索引操作来检查计数方法是否被调用。我打算使用 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
最佳答案
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。
关于ruby-on-rails - rr gem assert_received 相当于 mocha gem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31006721/