所以我有这个简单的 ruby 类:
class GetRequestList
def initialize(current_user, filter_hash)
@authorizer = RequestAuthorizer.new(current_user)
@filter = RequestFilter.new(filter_hash)
end
def generate
Request.send_chain(@authorizer.method_chain)
.send_chain(@filter.method_chain)
end
end
而且我想测试Request
接收两种send_chain
方法的隔离RequestAuthorizer
和RequestFilter
实现。为此,我正在尝试使用一些存根:
require 'test_helper'
class GetRequestListTest < ActiveSupport::TestCase
test "request should be filtered by filter and role" do
Request.expects(:send_chain).twice.returns([build(:request)])
RequestFilter.stubs(:new)
RequestFilter.any_instance.stubs(:method_chain).returns([])
RequestAuthorizer.stubs(:new)
RequestAuthorizer.any_instance.stubs(:method_chain).returns([])
assert GetRequestList.new(:current_user, :filter).generate.size == 1
end
end
你看看有什么问题。stubs(:new)
返回nil
并且没有实例RequestAuthorizer
和RequestFilter
实例变量,GetRequestList
我们得到一个错误。我不知道如何在实例变量上存根方法。有什么建议么?