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] = true
和 params[: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 }
]}
我有一个控制器:
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] = true
和 params[: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 }
]}