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

UseCaseMock for testing #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

UseCaseMock for testing #3

wants to merge 1 commit into from

Conversation

AlexB52
Copy link

@AlexB52 AlexB52 commented Aug 1, 2018

Testing

When a use case is used in a class to perform an action or query, it can be handy to use a mock for the use case when testing the class.

To do so, require 'use_case_mock' in your specs and use the UseCaseMock class to create the state of the use case after the perform action. This is particularly handy when you want to add errors and keep a stable use case behaviour.
Adding errors to the mock will make the use case failed? but not invalid? and won't raise if perform! is called.

require 'spec_helper'
require 'use_case_mock'

RSpec.describe MyParentClass do

  before do
    use_case = UseCaseMock.new(results: [])
    use_case.errors.add(:base, 'Could not fetch results from 3rd party API')

    allow(MyUseCase).to receive(:perform) { use_case }
  end

  describe '.upload' do
    it 'does something' do
      MyParentClass.upload('/some/random/file.txt')

      # ...
    end
  end
end

@AlexB52 AlexB52 requested a review from nigelramsay August 1, 2018 00:08
@AlexB52 AlexB52 force-pushed the feature/testing_mock branch from 1c48294 to 3daf43a Compare August 1, 2018 00:09
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.

2 participants