RSpec Mocks and Stubs in Plain English
One of the most common questions I see from beginners to Rails testing is what mocks and stubs are and when to use them. https://www.codewithjason.com/rspec-mocks-stubs-plain-english/ If you’re confused about mocks and stubs, you’re not alone. In my experience very few people understand them. In this post I’ll attempt to help clarify the matter, particularly in the context of Rails/RSpec.
I want to preface my explanation with a disclaimer. This post is partly derived from personal experience but it’s mostly derived from me just reading a bunch of posts online and reading a few books to try to understand mocks and stubs better. It’s entirely possible that what follows is not 100% accurate. But I’m risking the inaccuracy because I think the internet needs a clear, simple explanation of mocks and stubs and so far I haven’t been able to find one.
Here’s what we’re going to cover in this post: