Commit 7c7ed2c516b0e5b4e47af5da7440f8537293f7c7
1 parent
00eff51b
Exists in
master
and in
19 other branches
Better test instructions!
Showing
1 changed file
with
59 additions
and
4 deletions
Show diff stats
README.md
@@ -1689,21 +1689,76 @@ Product.search "ah", misspellings: {prefix_length: 2} # ah, no aha | @@ -1689,21 +1689,76 @@ Product.search "ah", misspellings: {prefix_length: 2} # ah, no aha | ||
1689 | 1689 | ||
1690 | ## Testing | 1690 | ## Testing |
1691 | 1691 | ||
1692 | -This section could use some love. | 1692 | +For performance, only enable Searchkick callbacks for the tests that need it. |
1693 | + | ||
1694 | +### Minitest | ||
1695 | + | ||
1696 | +Add to your `test/test_helper.rb`: | ||
1697 | + | ||
1698 | +```ruby | ||
1699 | +Searchkick.disable_callbacks | ||
1700 | + | ||
1701 | +# and reindex models | ||
1702 | +Product.reindex | ||
1703 | +``` | ||
1704 | + | ||
1705 | +And use: | ||
1706 | + | ||
1707 | +```ruby | ||
1708 | +class ProductTest < Minitest::Test | ||
1709 | + def setup | ||
1710 | + Searchkick.enable_callbacks | ||
1711 | + end | ||
1712 | + | ||
1713 | + def teardown | ||
1714 | + Searchkick.disable_callbacks | ||
1715 | + end | ||
1716 | + | ||
1717 | + def test_search | ||
1718 | + Product.create!(name: "Apple") | ||
1719 | + Product.search_index.refresh | ||
1720 | + assert_equal ["Apple"], Product.search("apple").map(&:name) | ||
1721 | + end | ||
1722 | +end | ||
1723 | +``` | ||
1693 | 1724 | ||
1694 | ### RSpec | 1725 | ### RSpec |
1695 | 1726 | ||
1727 | +Add to your `spec/spec_helper.rb`: | ||
1728 | + | ||
1696 | ```ruby | 1729 | ```ruby |
1697 | -describe Product do | ||
1698 | - it "searches" do | 1730 | +RSpec.configure do |config| |
1731 | + config.before(:suite) do | ||
1732 | + Searchkick.disable_callbacks | ||
1733 | + | ||
1734 | + # and reindex models | ||
1699 | Product.reindex | 1735 | Product.reindex |
1700 | - # test goes here... | 1736 | + end |
1737 | + | ||
1738 | + config.around(:each, search: true) do |example| | ||
1739 | + Searchkick.enable_callbacks | ||
1740 | + example.run | ||
1741 | + Searchkick.disable_callbacks | ||
1742 | + end | ||
1743 | +end | ||
1744 | +``` | ||
1745 | + | ||
1746 | +And use: | ||
1747 | + | ||
1748 | +```ruby | ||
1749 | +describe Product, search: true do | ||
1750 | + it "searches" do | ||
1751 | + Product.create!(name: "Apple") | ||
1752 | + Product.search_index.refresh | ||
1753 | + assert_equal ["Apple"], Product.search("apple").map(&:name) | ||
1701 | end | 1754 | end |
1702 | end | 1755 | end |
1703 | ``` | 1756 | ``` |
1704 | 1757 | ||
1705 | ### Factory Girl | 1758 | ### Factory Girl |
1706 | 1759 | ||
1760 | +Manually reindex after an instance is created. | ||
1761 | + | ||
1707 | ```ruby | 1762 | ```ruby |
1708 | product = FactoryGirl.create(:product) | 1763 | product = FactoryGirl.create(:product) |
1709 | product.reindex(refresh: true) | 1764 | product.reindex(refresh: true) |