1

我有一个控制器:

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

4

1 回答 1

0

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

let(:params) { Hash[
  person: {},
  'REQUEST_METHOD' => 'GET',
  quiz_id: 1,
  'rack.session' => { person_data_validated: true }
]}
于 2017-09-17T20:56:27.790 回答