繁体   English   中英

在Hanami 1.0.0上运行的Minitest以某种方式破坏了控制器中的数据

[英]Minitest run on hanami 1.0.0 somehow corrupts data in controller

我有一个控制器:

module Web::Controllers::Quiz
  class Test
    include Web::Action
    params QuizValidation

    def show(thing)
      thing
    end

    def call(params)
      if request.get?
        unless session.has_key?(:person_data_validated) && session[:person_data_validated]
          redirect_to routes.path(:person, params[:quiz_id])
        end
      end
    end

  end
end

我有一个规格测试:

require 'spec_helper'
require_relative '../../../../apps/web/controllers/quiz/test'

describe Web::Controllers::Quiz::Test do
  let(:action) { Web::Controllers::Quiz::Test.new }
  let(:params) { Hash[person: {}, quiz_id: 1] }
  let(:repository) { QuizRepository.new }
  let(:session) { Hash.new }

  before do
    # repository things
  end

  describe 'reloading page [GET]' do
    let(:params) { Hash[person: {}, quiz_id: 1, 'REQUEST_METHOD' => 'GET'] }

    it 'reloads current state if person data validated' do
      session[:person_data_validated] = true

      response = action.call(params)
      # there I debug values of minitest
      action.show(params[:quiz_id]).must_equal 'test'

      response[0].must_equal 200
    end
  end
end

PS:我应该说'REQUEST_METHOD' => 'GET'只是使它起作用的一个幸运猜测。

好的。 当我使用action.show(value)调试值时,我得到的正是预期的结果: session[:person_data_validated] = trueparams[:quiz_id] = 1

但是当我实际运行此测试时,它会显示错误:

Web::Controllers::Quiz::Test::reloading page [GET]#test_0002_reloads current state if person data validated:
Hanami::Routing::InvalidRouteException: No route (path) could be generated for :person - please check given arguments

好的。 当我在控制器中明确设置此行时:

redirect_to routes.path(:person, params[:quiz_id])

redirect_to routes.path(:person, 1)

一切正常。

我们可以忽略由于session[:person_data_validated]条件而不应执行的这一行代码。

帮助我了解我的失明并克服它。 如何使其运作?

编辑:我忘记了使用以下验证规则将params QuizValidation文件包括到控制器(现已包括)中:

module Web::Controllers::Quiz
  class QuizValidation < Web::Action::Params

    # arrays of described values
    REGIONS = ::I18n.t "web.quiz.person.regions"
    QUIZ_LANGUAGE_LEVELS = ::I18n.t "web.quiz.person.quiz_language_levels"
    LANGUAGES = ::I18n.t "languages"

    params do
      required(:person).schema do
        optional(:sex).filled(:str?, included_in?: ['male', 'female'])
        optional(:age).filled(:int?, included_in?: 1..100)
        optional(:profession).filled(:str?)
        optional(:region).filled(:str?, included_in?: REGIONS)
        optional(:residence_place).filled(:str?)
        optional(:birth_place).filled(:str?)
        optional(:nationality1).filled(:str?)
        optional(:nationality2).filled(:str?)
        optional(:spoken_languages).filled(:int?, included_in?: 1..100)
        optional(:native_language).filled(:str?, included_in?: LANGUAGES)
        optional(:communication_language).filled(:str?, included_in?: LANGUAGES)
        optional(:education_language).filled(:str?, included_in?: LANGUAGES)
        optional(:quiz_language_level).filled(:str?, included_in?: QUIZ_LANGUAGE_LEVELS)
      end
    end
  end
end

注释掉该文件后,我没有收到错误,但是我仍然进入if内,以便执行redirect_to routes.path(:person, params[:quiz_id])

路由器参数应作为常规参数传递,然后可以通过params规则进行验证。 会话声明如下所示。

let(:params) { Hash[
  person: {},
  'REQUEST_METHOD' => 'GET',
  quiz_id: 1,
  'rack.session' => { person_data_validated: true }
]}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM