سؤال

ولدي التصفية التالية تعريف:

# application_controller.rb
class ApplicationController < ActionController::Base
  before_filter :find_account

  private

    def find_account
      @current_account = Account.find_by_subdomain!(request.subdomains.first)
    end
end

ووفي اختباري:

# users_controller_test.rb
class UsersControllerTest < ActionController::TestCase
  setup do
    @request.host = "test.myapp.local"
  end
  # ...
end

والآن يعرف test باسم نطاق فرعي لحساب وهمية أنني تحميل مسبق لجميع الطلبات باستخدام factory_girl. ومع ذلك، هذا هو رمي خطأ كائن لا شيء، قائلا انrequest هو لا شيء. إزالة كتلة الإعداد يسبب كل من بلدي التجارب أن تفشل كما find_account لا يمكن العثور على حساب وبالتالي يلقي خطأ RecordNotFound.

وماذا أفعل الخطأ؟

هل كانت مفيدة؟

المحلول

وجرب هذا:

@request.env['HTTP_HOST'] = 'test.myapp.local'
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top