Rspec Any_Instance Deprecation: How to Fix It

RSpec any_instance deprecation: how to fix it?

Use allow_any_instance_of:

describe (".create") do
it 'returns error when...' do
allow_any_instance_of(User).to receive(:save).and_return(false)
post :create, user: {name: "foo", surname: "bar"}, format: :json
expect(response.status).to eq(422)
end
end

RSpec any_instance deprecation: how to fix it?

Use allow_any_instance_of:

describe (".create") do
it 'returns error when...' do
allow_any_instance_of(User).to receive(:save).and_return(false)
post :create, user: {name: "foo", surname: "bar"}, format: :json
expect(response.status).to eq(422)
end
end

How to Fix: deprecation warning of 'any_instance'

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

When I run the test rspec spec In the console, I'll get a deprecation warning

Write the test in a new style:

expect(item.price).to eq 212

BTW. it seems you might doing sth quite risk/confusing. Once you assign 200 to the attribute, it will be more than confusing to see another value returned by a getter with a same name. Have you considered leaving the original method alone and defining a new one instead (like price_with_vat)?

How to avoid deprecation warning for stub_chain in RSpec 3.0?

RSpec.configure do |config|
config.mock_with :rspec do |c|
c.syntax = [:should, :expect]
end
end

Notice that it's setting the rspec-mocks syntax, not the rspec-expectations syntax, as Paul's answer shows.

How to say any_instance should_receive any number of times in RSpec

There's a new syntax for this:

expect_any_instance_of(Model).to receive(:save).at_least(:once)


Related Topics



Leave a reply



Submit