Factory.Girl与Time.zone.today问题

时间:2014-10-15 05:35:12

标签: ruby-on-rails rspec factory-bot

当我尝试返回对象的当前实例时,我遇到了FactoryGirl的问题。我正在处理的对象具有以下范围:

scope :current, -> {activeAtDate}

scope :activeAtDate, lambda{ |date = Time.zone.today| where("? BETWEEN start_date and end_date", date)}

在我的工厂顶部.rb我有:

Time.zone = "Melbourne"

有问题的工厂是:

factory :focus do
    title "Test Focus Title"
    store
    pp Time.zone
    start_date Time.zone.today
    end_date Time.zone.today
end

factory :store do
    name "Test store"
    address "This is the address of the test store"
    number "98765432"

    factory :store_with_kpi do

        ignore do
            kpi_count 5
        end

        after(:create) do |store, evaluator|
            create_list(:kpi, evaluator.kpi_count, store: store)
        end

    end

    factory :store_with_focus do

        ignore do
            focus_count 5
        end

        after(:create) do |store, evaluator|
            create_list(:focus, evaluator.focus_count, store: store)
        end
    end
end

'pp Time.zone'输出是:

    <ActiveSupport::TimeZone:0x007fe7fd1bd630@current_period=
   #<TZInfo::TimezonePeriod: #<TZInfo::TimezoneTransitionDefinition: #<TZInfo::TimeOrDateTime:     1412438400>,#<TZInfo::TimezoneOffset: 36000,3600,AEDT>>,#<TZInfo::TimezoneTransitionDefinition: #    <TZInfo::TimeOrDateTime: 1428163200>,#<TZInfo::TimezoneOffset: 36000,0,AEST>>>,
     @name="Melbourne",
     @tzinfo=#<TZInfo::TimezoneProxy: Australia/Melbourne>,
     @utc_offset=nil>

最后是相关测试:

it "We can get all current focuses as JSON" do
    @store = FactoryGirl.create(:store_with_focus)
    @user = FactoryGirl.create(:user, store: @store)
    sign_in @user

    get :get_focus, {}

    #Of course we want to have a successful response
    expect(response).to be_success


    #But maybe, we also want the number of objects returned to be correct as well
    expect(json.count).to eq(5)
end

然而,如果我在控制器规范中输出创建的焦点,则使用start_date和end_date以UTC格式创建它们,因此我的规范失败。如果我能提供任何信息,请告诉我,我已经尝试过:

  • 向store_with_focus工厂添加时区,然后从控制器规范
  • 传递该时区
  • start_date {Time.zone.today}

提前感谢您的帮助。

0 个答案:

没有答案