如何修复:'any_instance' 的弃用警告
How to Fix: deprecation warning of 'any_instance'
我的问题
该方案有效,但在控制台中,当我 运行 测试 (bin/rspec
) 时,我收到此警告:
Deprecation Warnings:
Using any_instance
from rspec-mocks' old :should
syntax without explicitly enabling the syntax is deprecated. Use the new :expect
syntax or explicitly enable :should
instead.
Called from /home/wakematta/github/example/spec/features/aspec/features/premium_spec.rb:3:in `block (2 levels) in '.
If you need more of the backtrace for any of these deprecations to
identify where to make the necessary changes, you can configure
config.raise_errors_for_deprecations!
, and it will turn the
deprecation warnings into errors, giving you the full backtrace.
1 deprecation warning total
我的场景
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
include ExtraContent
end
app/controllers/concerns/extra_content.rb
module ExtraContent
extend ActiveSupport::Concern
included do
helper_method :extra_content?
end
def extra_content?
current_user.premium?
end
end
app/views/users/show.html.haml
%h1= @user.name
- if extra_content?
%span.premium PREMIUM
spec/features/premium_spec.rb
feature 'Premium features' do
scenario 'premium user can view extra content' do
ApplicationController.any_instance.stub(:extra_content?).and_return(true)
visit '/users/1'
expect(page).to have_content 'PREMIUM'
end
end
改变这个:
spec/features/premium_spec.rb
feature 'Premium features' do
scenario 'premium user can view extra content' do
ApplicationController.any_instance.stub(:extra_content?).and_return(true)
visit '/users/1'
expect(page).to have_content 'PREMIUM'
end
end
为此:
spec/features/premium_spec.rb
feature 'Premium features' do
scenario 'premium user can view extra content' do
allow_any_instance_of(ApplicationController).to receive(:extra_content?).and_return(true)
visit '/users/1'
expect(page).to have_content 'PREMIUM'
end
end
我的问题
该方案有效,但在控制台中,当我 运行 测试 (bin/rspec
) 时,我收到此警告:
Deprecation Warnings:
Using
any_instance
from rspec-mocks' old:should
syntax without explicitly enabling the syntax is deprecated. Use the new:expect
syntax or explicitly enable:should
instead. Called from /home/wakematta/github/example/spec/features/aspec/features/premium_spec.rb:3:in `block (2 levels) in '.If you need more of the backtrace for any of these deprecations to identify where to make the necessary changes, you can configure
config.raise_errors_for_deprecations!
, and it will turn the deprecation warnings into errors, giving you the full backtrace.1 deprecation warning total
我的场景
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
include ExtraContent
end
app/controllers/concerns/extra_content.rb
module ExtraContent
extend ActiveSupport::Concern
included do
helper_method :extra_content?
end
def extra_content?
current_user.premium?
end
end
app/views/users/show.html.haml
%h1= @user.name
- if extra_content?
%span.premium PREMIUM
spec/features/premium_spec.rb
feature 'Premium features' do
scenario 'premium user can view extra content' do
ApplicationController.any_instance.stub(:extra_content?).and_return(true)
visit '/users/1'
expect(page).to have_content 'PREMIUM'
end
end
改变这个:
spec/features/premium_spec.rb
feature 'Premium features' do
scenario 'premium user can view extra content' do
ApplicationController.any_instance.stub(:extra_content?).and_return(true)
visit '/users/1'
expect(page).to have_content 'PREMIUM'
end
end
为此:
spec/features/premium_spec.rb
feature 'Premium features' do
scenario 'premium user can view extra content' do
allow_any_instance_of(ApplicationController).to receive(:extra_content?).and_return(true)
visit '/users/1'
expect(page).to have_content 'PREMIUM'
end
end