PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 28, 2022

[FIXED] How to Fix: deprecation warning of 'any_instance'

 April 28, 2022     mocking, rspec, ruby-on-rails, ruby-on-rails-4, warnings     No comments   

Issue

My question

The scenario it works but in the console when I run the tests (bin/rspec) I get this warning:

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

My scenario

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

Solution

Change this:

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

For this:

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


Answered By - Mohamed Ziata
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing