RSpec mocks in Rails native tests (minitest)
Today I've Learned postI love RSpec but recently I’ve decided to build sideproject with 100% Rails 6 vanilla environment (that means no extra gems, just what is inside Rails 6, including the Minitest test environment)
Problem is Minitest stubs and mocks suuuucks !
If you are interested in Minitest mocks check this article
So I want to use Rails Minitest but RSpec mocks and this is how:
# Gemfile
# ...
group :test do
# ...
gem 'rspec-mocks'
end
# ...
note, no
rails-rspec
is not required
# test/test_helper
# ...
require 'rspec/mocks/minitest_integration'
# ...
require 'test_helper'
class AddPhoneControllerTest < ActionDispatch::IntegrationTest
test 'POST create add phone to inzerat when valid arguments should redirect to enter code path' do
post some_path, params: { phone: { country: 'sk', number: '908111222' } }
expect(SmsGatewayContract.contract)
.to receive(:send_sms)
.and_call_original
post_add_phone_to_inzerat
end
end
more resources:
Alternatives:
Entire blog website and all the articles can be forked from this Github Repo