Ruby on rails 名为inside controller的Rails Minitest存根类方法

Ruby on rails 名为inside controller的Rails Minitest存根类方法,ruby-on-rails,unit-testing,ruby-on-rails-4,minitest,Ruby On Rails,Unit Testing,Ruby On Rails 4,Minitest,在模拟请求时,如何存根在控制器内部调用的方法 我尝试的是: 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

在模拟请求时,如何存根在控制器内部调用的方法

我尝试的是:

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
需要“测试助手”
类确认ControllerTest
当我在存根中打印
User.u方法时,它会工作,但当我在控制器操作中使用该方法时,它不会工作。

试试看

e、 g.(来自他们的文件)

String.stub_any_instance(:length, 42) do
  assert_equal "hello".length, 42
end