如何在模拟请求时 stub 在 Controller 内部调用的方法?
我尝试过的:
require 'test_helper'
class ConfirmationsControllerTest < ActionController::TestCase
test 'dummy test' do
User.stub(:some_method, 'string') do
p User.some_method
get(:index)
end
end
end
class ConfirmationsController < ApplicationController
def index
p User.some_method
end
end
当我在 stub 中打印 User.some_method
时它有效,但是当我在我的 Controller 操作中使用该方法时它不起作用。
最佳答案
尝试 https://github.com/codeodor/minitest-stub_any_instance
例如(来自他们的文档)
String.stub_any_instance(:length, 42) do
assert_equal "hello".length, 42
end
关于ruby-on-rails - 在 Controller 内部调用的 Rails Minitest Stub 类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29563984/