Ruby on rails 在rspec3中,用一个模拟对象来存根类方法的正确方法,该对象上设置了一个期望值

Ruby on rails 在rspec3中,用一个模拟对象来存根类方法的正确方法,该对象上设置了一个期望值,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,这里有一个工作测试,Rspec告诉我现在是错误的,因为存根已弃用: it 'paginates the right number of times' do mock_thing = mock_model(User) User.stub!(:page).and_return(mock_thing) mock_thing.should_receive(:per).with(50) get :index end 正确的方法是什么?我尝试了下面的代码,但失败了: it 'paginat

这里有一个工作测试,Rspec告诉我现在是错误的,因为
存根已弃用:

it 'paginates the right number of times' do
  mock_thing = mock_model(User)
  User.stub!(:page).and_return(mock_thing)
  mock_thing.should_receive(:per).with(50)
  get :index
end
正确的方法是什么?我尝试了下面的代码,但失败了:

it 'paginates the right number of times' do
  mock_thing = mock_model(User)
  mock_thing.should_receive(:per).with(50)
  User.stub(:page, mock_thing) do
    get :index
  end
end

# Error:
# RSpec::Mocks::MockExpectationError: Double "User_1001" received unexpected 
# message :[]= with (:expected_from, ...Stacktrace removed

问题陈述是:

User.stub!(:page).and_return(mock_thing)
这需要对
对象
进行猴子补丁,以支持
存根
方法

您可以显式启用
should
语法来绕过弃用警告,但用新语法表达这一点的方法是:

allow(User).to receive(:page).and_return(mock_thing)
我还没有复习你剩下的例子的合理性