Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: callback on mock spec helpers #24

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

michaelwautier
Copy link
Contributor

@michaelwautier michaelwautier commented Aug 28, 2024

In some cases, you want to mock your command in the spec, but your spec still requires some data that would have been updated through the command.

I suggest adding the possibility to run a code block when the command mock has ran.

Example of use case:

# controller

class FoosController
  def update
    # [...]

    FooUpdater.call(current_user, params) # let's say this command does updates on another model, but the result is not related
    if bar.baz?
      BarBazHandler.call
    else
      QuxHandler.call
    end

    # [...]
  end
end

# spec

describe 'PATCH update' do
  subject(:update_foo) { patch('...') }

  let(:bar) { create(:bar, baz: false) }

  context 'when baz = true' do
    before { mock_successful_command(FooUpdater, result: build(:foo)) { bar.update!(baz: true) } }

    it 'handles bar_baz' do
      create_foo
      expect(BarBazHandler).to have_received(:call)
    end
  end

  context 'when baz = false' do
    before { mock_successful_command(FooUpdater, result: build(:foo)) }

    it 'handles qux' do
      create_foo
      expect(QuxHandler).to have_received(:call)
    end
  end
end

@michaelwautier michaelwautier requested a review from a team as a code owner August 28, 2024 12:10
@endenis endenis self-requested a review January 27, 2025 14:14
@AlexLamande
Copy link
Contributor

hey 👋
i'm closing/re-opening the PR to re-launch ci
@endenis will perform a review soon

@AlexLamande AlexLamande reopened this Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants