Note that there are some explanatory texts on larger screens.

plurals
  1. POUninitialized Constant error Loading a class/module in a Rails initializer
    text
    copied!<p>I'm working on integrating Stripe's webhooks into a Rails app using <a href="https://github.com/integrallis/stripe_event" rel="nofollow">https://github.com/integrallis/stripe_event</a>. I'm struggling to get my code to working according to the example in the gem's docs whereby an initializer is used to dictate which code responds to a particular event. It seems that Rails isn't (auto)loading my module in the initializer.</p> <p>I'm configuring the autoload path properly:</p> <pre><code># config/application.rb config.autoload_paths += %W(#{config.root}/lib) </code></pre> <p>The stripe initializer:</p> <pre><code>#config/initializers/stripe.rb stripe_config = YAML.load_file(Rails.root.join('config', 'stripe.yml'))[Rails.env] Stripe.api_key = stripe_config["secret_key"] STRIPE_PUBLIC_KEY = stripe_config["publishable_key"] StripeEvent.setup do # Not sure if I need this to load my module require 'stripe_event_handlers' # =&gt; true subscribe 'customer.subscription.created' do |event| StripeEventHanders.handle_customer_subscription_created(event) # Define subscriber behavior end end </code></pre> <p>Here's my custom module (though I've tried it as a class too):</p> <pre><code>#lib/stripe_event_handlers.rb module StripeEventHandlers def handle_customer_subscription_created(event) # Define subscriber behavior puts event end end </code></pre> <p>This is my test:</p> <pre><code>require 'test_helper' # --- Run this in the console to get event response for mocking --- #serialized_object = YAML::dump(Stripe::Event.retrieve('evt_0Cizt88YP0nCle')) #filename = Rails.root.join('test/fixtures/stripe_objects', 'customer_subscription_created.yml') #File.open(filename, 'w') {|f| f.write(serialized_object) } class StripeEvent::WebhookControllerTest &lt; ActionController::TestCase def test_mock_event event_id = 'evt_0Cizt88YP0nCle' event = YAML.load_file(Rails.root.join('test/fixtures/stripe_objects', 'customer_subscription_created.yml')) Stripe::Event.expects(:retrieve).with(event_id).returns(event) assert_equal Stripe::Event.retrieve(event_id), event end def test_customer_subscription_created_webhook event_id = 'evt_0Cizt88YP0nCle' event = YAML.load_file(Rails.root.join('test/fixtures/stripe_objects', 'customer_subscription_created.yml')) Stripe::Event.expects(:retrieve).at_most(2).with(event_id).returns(event) # This should be a raw post request but that doesn't seem to come through # on the stripe_event / rails side in the params hash. For testing # purposes, we can just use a get request as the route doesn't specify an # HTTP method. get :event, :use_route =&gt; :stripe_event, :id =&gt; event_id assert_response :success end end </code></pre> <p>And here's my test result failure:</p> <pre><code>StripeEvent::WebhookControllerTest ERROR (0:00:00.043) test_customer_subscription_created_webhook uninitialized constant StripeEventHanders @ config/initializers/stripe.rb:10:in `block (2 levels) in &lt;top (required)&gt;' PASS (0:00:00.053) test_mock_event Finished in 0.055477 seconds. 2 tests, 1 passed, 0 failures, 1 errors, 0 skips, 2 assertions </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload