I have a 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
I have a spec test:
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: I should say that 'REQUEST_METHOD' => 'GET'
was just a lucky guess to make it work.
Okay. When I debug values with action.show(value)
I get exactly what is expected: session[:person_data_validated] = true
and params[:quiz_id] = 1
.
But when I actually run this test, it prints an error:
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
Okay. When I explicitly set this line in controller:
redirect_to routes.path(:person, params[:quiz_id])
to
redirect_to routes.path(:person, 1)
everything works fine.
We can omit that this line of code even shouldn't be executed because of session[:person_data_validated]
condition.
Help me understand my blindness and overcome it. How to make it work?
EDIT:
I forgot to include the params QuizValidation
file to the controller (now included) with the following validation rules:
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
With this file commented out I don't get the error but I still go inside the if
so that redirect_to routes.path(:person, params[:quiz_id])
gets executed.
The router param should be passed as a normal parameter and then it can be validated by the params
rules. The session declaration is shown below.
let(:params) { Hash[
person: {},
'REQUEST_METHOD' => 'GET',
quiz_id: 1,
'rack.session' => { person_data_validated: true }
]}