#[1]gnikyt feed [2]gnikyt Code ramblings. Testing with shopify_app /* Jun 21, 2016 — 2.3KB */ [3]shopify_app is a great Gem to quickly build a Shopify App with Rails. It handles all the verifications, authorizations, webhook, and more; leaving you to focus on developing your app. This post is to serve as a quick tip on how to unit-test your app with the Gem. Other than the install page, most likely your app will require a shop to be logged in for all routes. shopify_app, upon installation will create a fixture file in test/fixtures/shops.yml of your Rails application. Inspecting it you will see it’s filled with one shop, regular-shop.myshopify.com, and a simple token. When running bundle exec rake test (after migrations are done for a test environment of course), it will populate the shop database table with the fixture data in shop.yml. Now that we have a shop we can “log into”, let’s create a support module for the unit tests. Create a file called shopify_session_support.rb inside test/support. # test/support/shopify_session_support.rb module ShopifySessionSupport # Make this module concernable extend ActiveSupport::Concern included do # On include, tell TestCase to use this setup block setup do # Set the shop to the test shop from fixtures # The key is `:shopify` and `:shopify_domain` which shopify_app needs # See: https://github.com/Shopify/shopify_app/blob/c7e50247a72a52b1d1e4d90 09ba997196a64e7e8/lib/shopify_app/login_protection.rb#L22 session[:shopify] = shops(:regular_shop).id session[:shopify_domain] = shops(:regular_shop).shopify_domain end end end Next, include your new support module in test_helper.rb before class ActiveSupport::TestCase with: # Include support modules Dir[Rails.root.join('test/support/**/*.rb')].each { |f| require f } Finally, in any of your controller tests where you require an authenticated shop, simply add include ShopifySessionSupport to your class as such: # ... class ImageControllerTest < ActionController::TestCase include ShopifySessionSupport # ... end Running bundle exec rake test should now pass your tests with the shop. [4]MD | [5]TXT | [6]CC-4.0 This post is 8 years old and may contain outdated information. __________________________________________________________________ [7]Ty King Ty King A self-taught, seasoned, and versatile developer from Newfoundland. Crafting innovative solutions with care and expertise. See more [8]about me. [9]Github [10]LinkedIn [11]CV [12]RSS * * * * * * * * * * References Visible links: 1. /rss.xml 2. / 3. https://github.com/Shopify/shopify_app 4. /testing-with-shopify-app/index.md 5. /testing-with-shopify-app/index.txt 6. https://creativecommons.org/licenses/by/4.0/ 7. /about 8. /about 9. https://github.com/gnikyt 10. https://linkedin.com/in/gnikyt 11. /assets/files/cv.pdf 12. /rss.xml Hidden links: 14. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb1-1 15. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb1-2 16. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb1-3 17. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb1-4 18. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb1-5 19. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb1-6 20. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb1-7 21. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb1-8 22. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb1-9 23. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb1-10 24. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb1-11 25. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb1-12 26. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb1-13 27. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb1-14 28. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb1-15 29. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb1-16 30. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb2-1 31. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb2-2 32. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb3-1 33. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb3-2 34. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb3-3 35. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb3-4 36. localhost/tmp/lynxXXXXEh2dga/L767744-5894TMP.html#cb3-5