【发布时间】:2020-09-10 08:38:19
【问题描述】:
我是 Crystal 和 Amber 的新手,我在测试非公共路线时遇到了问题。我使用了 Amber 身份验证生成器,然后为 Job 实体生成了一个脚手架,并将相关路由添加到 routes :auth 块。 当我打开浏览器并尝试直接进入工作路线时,一切都按预期工作,我被重定向到登录页面。
但是当我为 JobsController 执行生成的测试时,我收到以下错误:
1) JobControllerTest renders job index template
Missing hash key: :auth (KeyError)
from ../../.asdf/installs/crystal/0.35.1/src/hash.cr:1030:9 in ‘[]’
from lib/amber/src/amber/pipes/pipeline.cr:19:15 in ‘call’
from lib/garnet_spec/src/garnet_spec/controller/test.cr:25:7 in ‘process_request’
from spec/controllers/job_controller_spec.cr:20:1 in ‘get’
from spec/controllers/job_controller_spec.cr:39:5 in ‘->’
from ../../.asdf/installs/crystal/0.35.1/src/primitives.cr:255:3 in ‘internal_run’
from ../../.asdf/installs/crystal/0.35.1/src/spec/example.cr:33:16 in ‘run’
from ../../.asdf/installs/crystal/0.35.1/src/spec/context.cr:18:23 in ‘internal_run’
from ../../.asdf/installs/crystal/0.35.1/src/spec/context.cr:330:7 in ‘run’
from ../../.asdf/installs/crystal/0.35.1/src/spec/context.cr:18:23 in ‘internal_run’
from ../../.asdf/installs/crystal/0.35.1/src/spec/context.cr:147:7 in ‘run’
from ../../.asdf/installs/crystal/0.35.1/src/spec/dsl.cr:270:7 in ‘->’
from ../../.asdf/installs/crystal/0.35.1/src/primitives.cr:255:3 in ‘run’
from ../../.asdf/installs/crystal/0.35.1/src/crystal/main.cr:45:14 in ‘main’
from ../../.asdf/installs/crystal/0.35.1/src/crystal/main.cr:114:3 in ‘main’
routes.cr
routes :auth do
...
resources "jobs", JobController
end
JobControllerTest.cr
...
class JobControllerTest < GarnetSpec::Controller::Test
getter handler : Amber::Pipe::Pipeline
def initialize
@handler = Amber::Pipe::Pipeline.new
@handler.build :web do
plug Amber::Pipe::Error.new
plug Amber::Pipe::Session.new
plug Amber::Pipe::Flash.new
end
@handler.prepare_pipelines
end
end
describe JobControllerTest do
subject = JobControllerTest.new
it “renders job index template” do
Job.clear
response = subject.get “/jobs” # -> line 39 where the error happens
response.status_code.should eq(302)
response.body.should contain(“jobs”)
end
end
...
我在 Ambers 文档和 Google 上都没有找到任何信息。我的问题如下:
- 我应该如何提供身份验证数据?为什么注销时应用程序会重定向?
- 对于控制器规格是否有任何测试助手可以让用户登录以便可以 使用经过身份验证的用户进行测试?
【问题讨论】:
标签: authentication integration-testing keyerror crystal-lang amber-framework