Tag: testng

如何在Rails 5测试控制器中测试或绕过Basic Auth

我想在Rails 5中测试激活基本身份validation的控制器。 当前官方指令 (截至2018-10-14)中解释的方式由于某种原因不起作用。 问题与解答 “ 在Rails 2.2+中测试HTTP基本身份validation ”对于Rails 5来说似乎太旧了(至少对于默认值而言)。 这是重现案例的简化示例。 我通过全新安装的Rails(最新的稳定版本5.2.1)通过脚手架制作了一个文章模型和相关资源: bin/rails g scaffold Article title:string content:text 并根据官方指南将基本的authfunction添加到控制器; 那么ArticlesController是这样的,当然有效: # /app/controllers/articles_controller.rb class ArticlesController < ApplicationController http_basic_authenticate_with name: "user1", password: "pass" before_action :set_article, only: [:show, :edit, :update, :destroy] def index @articles = Article.all end end 官方说明解释了测试基本认证的方法; 你在控制器的测试文件中的setup块中添加了request.headers[‘Authorization’] ,我做了: # /test/controllers/articles_controller_test.rb require ‘test_helper’ class ArticlesControllerTest < […]