最小的hanami 1.0.0运行会以某种方式破坏控制器中的数据

时间:2017-09-16 17:38:35

标签: ruby minitest hanami

我有一个控制器:

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])被执行。

1 个答案:

答案 0 :(得分:0)

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

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