Mock Expectations
A mock object retains the basic idea of the stubâreturning a specified value without actually calling a live methodâand adds the requirement that the specified method must be called during the test. In other words, a mock is like a stub with attitude, expectingânay, demandingâthat its parameters be matched in the test or else we get a test failure.
In RSpec you use the expect method to create mock expectations. This can be applied to full or partial doubles:
â | it â"expects stuff"â âdoâ |
â | mocky = double(â"Mock"â) |
â | expect(mocky).to receive(:name).and_return(â"Paul"â) |
â | expect(mocky).to receive(:weight).and_return(100) |
â | expect(mocky.name).to eq(â"Paul"â) |
â | âendâ |
This test fails: ...
Get Rails 4 Test Prescriptions now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.