Rspec告诉我“ .update”'方法不确定

发布于 2025-02-03 07:43:45 字数 1969 浏览 0 评论 0原文

这是主题解析器。每次为学生执行此查询时,我都在尝试更新Learning_path表中的字段。

class Resolvers::Topic < GraphQL::Schema::Resolver
  type Types::TopicType, null: true
  description 'Returns the topic information'
  argument :title, String, required: true

  def resolve(title:)
    user = context[:current_user]
    ability = Ability.for(user)
    topic = Topic.find_by(title: title)

    if %w[student teacher admin].include?(user&.role) && ability.can?(:read, Topic)
      if user&.role == 'student'
        user.userable.learning_paths.find_by(subject_id: topic.subject_id).update(visited_at: DateTime.now)
      end

      if %w[student teacher].include?(user&.role) && !user&.userable&.subjects&.include?(topic.subject)
        raise GraphQL::ExecutionError, 'This topic is not avaiable for you.'
      end

      topic
    else
      raise GraphQL::ExecutionError, 'You can't access this information.'
    end
  end
end

它有效,但RSPEC正在使用此错误:

 1) LmsApiSchema topic when there's a current user returns an error if the student is not subscribe
     Failure/Error: user.userable.learning_paths.find_by(subject_id: topic.subject_id).update(visited_at: DateTime.now)
     
     NoMethodError:
       undefined method `update' for nil:NilClass

主题RSPEC测试,该测试未能测试:

context 'when there\'s a current user' do
      let!(:student) { create(:student) }
      let!(:user) { create(:user, userable: student) }
      let!(:subject_a) { create(:subject) }
      let!(:topic) { create(:topic, subject_id: subject_a.id) }
      let!(:question) { create(:question, topic_id: topic.id) }

      before do
        create(:option, question_id: question.id)
        prepare_context({ current_user: user })
      end

      it 'returns an error if the student is not subscribe' do
        expect(graphql!['errors'][0]['message']).to eq('This topic is not avaiable for you.')
      end

This is the topic resolver. I'm trying to update a field in learning_path table each time this query is executed for a student.

class Resolvers::Topic < GraphQL::Schema::Resolver
  type Types::TopicType, null: true
  description 'Returns the topic information'
  argument :title, String, required: true

  def resolve(title:)
    user = context[:current_user]
    ability = Ability.for(user)
    topic = Topic.find_by(title: title)

    if %w[student teacher admin].include?(user&.role) && ability.can?(:read, Topic)
      if user&.role == 'student'
        user.userable.learning_paths.find_by(subject_id: topic.subject_id).update(visited_at: DateTime.now)
      end

      if %w[student teacher].include?(user&.role) && !user&.userable&.subjects&.include?(topic.subject)
        raise GraphQL::ExecutionError, 'This topic is not avaiable for you.'
      end

      topic
    else
      raise GraphQL::ExecutionError, 'You can't access this information.'
    end
  end
end

It works but the rspec is failling with this error:

 1) LmsApiSchema topic when there's a current user returns an error if the student is not subscribe
     Failure/Error: user.userable.learning_paths.find_by(subject_id: topic.subject_id).update(visited_at: DateTime.now)
     
     NoMethodError:
       undefined method `update' for nil:NilClass

topic rspec test that fails in testing:

context 'when there\'s a current user' do
      let!(:student) { create(:student) }
      let!(:user) { create(:user, userable: student) }
      let!(:subject_a) { create(:subject) }
      let!(:topic) { create(:topic, subject_id: subject_a.id) }
      let!(:question) { create(:question, topic_id: topic.id) }

      before do
        create(:option, question_id: question.id)
        prepare_context({ current_user: user })
      end

      it 'returns an error if the student is not subscribe' do
        expect(graphql!['errors'][0]['message']).to eq('This topic is not avaiable for you.')
      end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏尔 2025-02-10 07:43:45

刚锻炼。添加了&amp;在设置避免零错误的方法之前。
多亏了 this

user.userable.learning_paths.find_by(subject_id: topic.subject_id)&.update(visited_at: DateTime.now)

Just worked out. Added the & before setting the method for avoiding the nil error.
Figured it out thanks to this

user.userable.learning_paths.find_by(subject_id: topic.subject_id)&.update(visited_at: DateTime.now)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文