Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Refactored sell_book
def sell_book
@book = Book.find(params[:id])

Bartuz commented Nov 1, 2015

Sorry for writing here but comments under the post are disabled.

You wrote comment to this snippet:

The controller is easier to test. When before we would have to have two tests, after our change is done we need only one.

How test for this action would look like? Do you just assert that BookSellingService received sell_book message with proper book?

describe 'GET #sell_book' do
  let(:book) { create(:book) }
  it 'calls selling book service (which is dependency of this controller, right?)' do
    get :sell_book, id:
    expect(BookSellingService).to receive(:sell_book).with(book)

Is that sufficient enough? (assuming you have unit tests for BookSellingService ).

Other scenario I'm thinking of is writing: receive(:sell_book).with(book).and_call_original?. But in that case I would have stub any dependencies of BookSellingService, is that right?

Let's say that BookSellingService uses courier (e.g. FedEx) API and needs to call it.
In that case would we have to add before { allow(FedEx).to receive(:ship!).and_return (true) } to this test case? IMHO controller test for this action shouldn't have any idea of internal BookSellingService implementation but I'm not highly concerned about it.

How would you write test for it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment