未初始化的常量錯誤在Rails初始值設定項中加載類/模塊

[英]Uninitialized Constant error Loading a class/module in a Rails initializer


I'm working on integrating Stripe's webhooks into a Rails app using https://github.com/integrallis/stripe_event. 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.

我正在使用https://github.com/integrallis/stripe_event將Stripe的webhooks集成到Rails應用程序中。我正在努力讓我的代碼按照gem文檔中的示例工作,其中初始化程序用於指示哪些代碼響應特定事件。似乎Rails沒有(自動)在初始化程序中加載我的模塊。

I'm configuring the autoload path properly:

我正在配置自動加載路徑:

# config/application.rb

config.autoload_paths += %W(#{config.root}/lib)

The stripe initializer:

條帶初始化器:

#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' # => true

  subscribe 'customer.subscription.created' do |event|
    StripeEventHanders.handle_customer_subscription_created(event) # Define subscriber behavior
  end
end

Here's my custom module (though I've tried it as a class too):

這是我的自定義模塊(雖然我也嘗試過它作為一個類):

#lib/stripe_event_handlers.rb

module StripeEventHandlers

  def handle_customer_subscription_created(event) # Define subscriber behavior
    puts event
  end
end

This is my test:

這是我的測試:

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 < 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 => :stripe_event, :id => event_id
    assert_response :success
  end
end

And here's my test result failure:

這是我的測試結果失敗:

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 <top (required)>'


     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

1 个解决方案

#1


1  

You are just missing the letter l in StripeEventHandlers.

你只是錯過了StripeEventHandlers中的字母l。

subscribe 'customer.subscription.created' do |event|
  StripeEventHanders.handle_customer_subscription_created(event)
end

Also, handle_customer_subscription_created should be defined as a class method:

此外,handle_customer_subscription_created應定義為類方法:

module StripeEventHandlers

  def self.handle_customer_subscription_created(event) # Define subscriber behavior
    puts event
  end
end

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/08/20/7254e6cf4d4b5ffbff8831238e02b8a7.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com