فشل الاختبار: sign_in(Fabricate(:user))

I’ve got a spec that started failing a few days ago. It looks like it’s due to something about timzone? I don’t see a way that it’s my plugin’s fault, but maybe I’m missing something?

require 'rails_helper'

describe TopicDefaultTag::ActionsController do
  before do
    Jobs.run_immediately!
  end

  it 'can list' do
    sign_in(Fabricate(:user))
    get "/topic-default-tag/list.json"
    expect(response.status).to eq(200)
  end
end
Running Rspec: plugins/discourse-topic-default-tag/spec/requests/actions_controller_spec.rb
Loading plugins while running specs

An error occurred while loading ./plugins/discourse-topic-default-tag/spec/requests/actions_controller_spec.rb.
Failure/Error: UserOption.create!(user_id: id)

NoMethodError:
  undefined method `timezone' for #<UserOption:0x000055dd7af16ca8>
  Did you mean?  timeout
# ./app/models/user.rb:1343:in `create_user_option'
# (eval):19:in `block (2 levels) in run_file'
# ./spec/rails_helper.rb:79:in `<top (required)>'
# ./plugins/discourse-topic-default-tag/spec/requests/actions_controller_spec.rb:1:in `require'
# ./plugins/discourse-topic-default-tag/spec/requests/actions_controller_spec.rb:1:in `<top (required)>'
No examples found.


Finished in 0.00005 seconds (files took 3.52 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
إعجاب واحد (1)

That sounds like you’re missing the timezone column in your user_options table. Have you run migrations recently in your test database? RAILS_ENV=test bin/rake db:migrate

إعجابَين (2)

Why, no! No I haven’t. I ran migrations on the dev database, but not test. Thanks, David!

I don’t understand what’s going on now, but it’s likely not as novice as this problem. :wink:

إعجابَين (2)

You should get a message when you’re missing migrations

But this will only work if you’re using the rails_helper. I suspect you need to add require "rails_helper" to the top of your spec file. That might resolve the other issues you’re seeing as well.

Edit: hmmm… maybe we should add --require rails_helper to the .rspec file so that it doesn’t need to be added manually :thinking:

إعجابَين (2)

That sounds like the kind of stupid error that I would make! Alas, I do have require 'rails_helper'.

Maybe runing ./bin/rake autospec rather than bundle exec rake autospec was my problem, but it’s still failing at travis, and now this spec seems to be doing a bunch of stuff that I don’t understand for my one little spec, but I’ll just wait and see what happens.

Thanks again.

إعجابَين (2)

أواجه مشكلة في دالة المساعدة sign_in، وأي مساعدة ستكون موضع تقدير كبير! :exploding_head: :

عند استخدامها في هذا الاختبار، يبدو أنها لا تُرجع مستخدمًا حاليًا (current_user) وتفشل برقم حالة 403 (بدلاً من 200).

https://github.com/paviliondev/discourse-follow/blob/8920cd7a4cb99ac5e98405fead8daa66/spec/requests/follow_controller_spec.rb#L33

أقوم بتشغيل الاختبار على النحو التالي لعزله، لكنه لا يعمل في أي من الحالتين:

LOAD_PLUGINS=1 RAILS_ENV=test rspec plugins/discourse-follow/spec/requests/follow_controller_spec.rb:32

إذا أضفت byebug في وحدة التحكم (controller) المقابلة وتحققت من قيمة current_user، فإنها تكون فارغة (nil) (ومن هنا يأتي خطأ 403 على ما أظن!):

Randomized with seed 50945

[5, 14] in /home/merefield/code/discourse-follow/app/controllers/follow/follow_controller.rb
    5:   def update
    6:     params.require(:username)
    7:     params.require(:follow)
    8:     byebug
    9:
=> 10:     raise Discourse::InvalidAccess.new unless current_user
   11:     raise Discourse::InvalidParameters.new if current_user.username == params[:username]
   12:
   13:     if user = User.find_by(username: params[:username])
   14:       updater = Follow::Updater.new(current_user, user)
(byebug) current_user
nil

هل لديك في الأعلى ينشئ ذلك المستخدم؟ (لستُ جيدًا بما يكفي لتذكر ما يُسمى أو كيفية القيام بذلك بالضبط. أوه، ربما منجّم؟ هل قمتَ بصياغة ذلك المستخدم؟

(أو ربما لم أرَ ذلك، لأنني أستخدم هاتفي)

إعجاب واحد (1)

نعم، تم إنشاء المستخدم بنجاح. تم تهيئة كائن المستخدم. المشكلة تكمن داخل طريقة sign_in أو تتعلق بالبيئة؟ (لكن ذلك يجب أن يكون خاضعًا للتحكم)

إعجاب واحد (1)

بعد مزيد من التحقيق، وجدت ما يلي:

في def current_user (discourse/lib/auth/default_current_user_provider.rb at 1472e47aae5bfdfb6fd9abfe89beb186c751f514 · discourse/discourse · GitHub)

@env.key?(CURRENT_USER_KEY) هي true

لذلك تأخذ current_user قيمة @env[CURRENT_USER_KEY]

ومع ذلك، فإن قيمتها هي nil عندما يتم استدعاء هذا السطر من المتحكم الخاص بي أثناء تشغيل الاختبار:

  def update
    params.require(:username)
    params.require(:follow)

    raise Discourse::InvalidAccess.new unless current_user

غير متأكد من سبب كون @env[CURRENT_USER_KEY] تساوي nil بعد تسجيل دخول مستخدم داخل الاختبار.

لاحظت أنه أثناء تشغيل الاختبار، يتم استدعاء current_user عدة مرات خلال اختبار واحد فقط، وفي نقطة معينة تكون هذه السمة لها قيمة، ولكن ليس في كل استدعاء، وليس في الوقت الذي يهم.

هل قمت بتثبيت أي إضافات أخرى قد تتداخل مع كائن current_user؟ قمت باستنساخ discourse-follow وتعمل هذه المواصفات بالنسبة لي:

❯ LOAD_PLUGINS=1 bin/rspec plugins/discourse-follow/spec/requests/follow_controller_spec.rb   

Randomized with seed 28704
...

Finished in 0.45075 seconds (files took 3.34 seconds to load)
3 examples, 0 failures

Randomized with seed 28704
إعجابَين (2)

شكرًا لك على التحقق يا ديفيد! إذن يجب أن يكون هناك شيء غريب في إعدادات التطوير الخاصة بي!

لا، لا توجد إضافات أخرى مثبتة (باستثناء الإضافات المدمجة واستكشف البيانات)، ولكن بناءً على أنه يعمل معك، فقد ألهمتني لإعداد مثيل Docker جديد ونظيف للتطوير ومحاولة تشغيله بنجاح هناك، تحياتي! :beers:

إعجاب واحد (1)

نعم، تم العمل على تطوير Docker:

d/rake plugin:spec["discourse-follow"]

شكرًا لك!!

3 إعجابات

تم إغلاق هذا الموضوع تلقائيًا بعد 20 ساعة. لم يعد مسموحًا بالردود الجديدة.