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
Bash: /Home/Xxx/.Rvm/Scripts/Rvm: No Such File or Directory
Ruby, Using Regex to Find Something in Between Two Strings
How to Calculate 32 Bit Crc in Ruby on Rails
How to Create Temp Dir in Ruby
Set Default Stage with Capistrano 3
Ruby Gems in Stand-Alone Ruby Scripts
Perform One Validation Only If All Other Validations Pass
How to Detect Browser Type and Its Version
How to Connect to Postgresql Database After Upgrading to Yosemite 10.10
How to Determine Leap Year in Ruby
Ruby: How to Find Out If a Character Is a Letter or a Digit
Rails 4 Update Nested Attributes
Ruby 2.0.0 String#Match Argumenterror: Invalid Byte Sequence in Utf-8
Omniauth Facebook Expired Token Error
Ruby - Remove Pattern from String
Cross-Platform Means of Getting User's Home Directory in Ruby
Why Is "❨╯°□°❩╯︵┻━┻" with Such an Encoding Used for a Method Name