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

Test hooks for MiniTest based suites? #392

Closed
robacarp opened this issue May 24, 2016 · 2 comments
Closed

Test hooks for MiniTest based suites? #392

robacarp opened this issue May 24, 2016 · 2 comments

Comments

@robacarp
Copy link

Hey Toptal, Nice gem you've got here. I was pleasantly surprised several times while evaluating adding it to our stack, particularly when I saw the decidedly short list of runtime dependencies. Nice job!

As I add it to our stack, I notice that you've provided a nice set of test hooks for rspec, but I don't see anything for MiniTest. Is there anything out there?

If not, is there interest in providing a minitest helper similar to the rspec helper?

@pyromaniac
Copy link
Contributor

Hey, Robert! Thanks for your comment. Sure we are interested, why not? Just submit a PR.

@robacarp
Copy link
Author

@pyromaniac I took a rough pass at it yesterday and came up with this before running into a snag that looks sort of like #325, but I'll sort that out elsewhere. I'll submit a pull later but I wanted to run this pattern past your radar early on. I tried to pull out some of the functionality which can be shared between the spec helper and a minitest helper into a storage class that can sit on the side.

Thoughts / comments welcome.

# example test
class UserIndexTest < MiniTest::TestCase
  include SearchTestHelper

  test 'creating a user causes the index to update' do
    user_id = nil
    updates = assert_reindexes UsersIndex::User do
      user = create(:registered_user)
      user_id = user.id
    end

    # more to be done here, but something like:
    assert_includes updates.indexes_for(UsersIndex::User), user_id
  end

end
class SearchIndexCatcher
  def initialize
    @mutations = {}
  end

  def index(type:, update:)
    if body = update[:delete]
      mutation_for(type).deletes << body[:_id]
    elsif body = update[:index]
      mutation_for(type).indexes << body
    end
  end

  def indexes_for index
    mutation_for(index).indexes
  end

  def deletes_for index
    mutation_for(index).deletes
  end

  private
  def mutation_for type
    @mutations[type] ||= OpenStruct.new(indexes: [], deletes: [])
  end
end
module SearchTestHelper
  def assert_reindexes index, &test_actions
    type = Chewy.derive_type index
    catcher = SearchIndexCatcher.new

    bulk_mock = -> (*stuff) do
      Array.wrap(stuff).map {|y| y[:body] }.flatten.each do |update|
        catcher.index type: self, update: update
      end

      {}
    end

    type.stub :bulk, bulk_mock do
      Chewy.strategy(:atomic) do
        test_actions.call
      end
    end

    catcher
  end
end

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

No branches or pull requests

2 participants