首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在 RSpec 测试中跳过 Rails http_basic_authenticate_with

在 RSpec 测试中,如果你想跳过 Rails 中的 http_basic_authenticate_with 方法,可以使用以下方法:

  1. 在测试用例中添加 beforearound 钩子,以便在测试运行之前或之时禁用 HTTP 基本身份验证。
代码语言:ruby
复制
describe "GET index" do
  around do |example|
    Rails.application.config.middleware.disable :http_basic_authenticator
    example.run
    Rails.application.config.middleware.enable :http_basic_authenticator
  end

  it "returns http success" do
    get :index
    expect(response).to have_http_status(:success)
  end
end
  1. 使用 Rack::Test::UploadedFile 上传一个包含正确用户名和密码的文件,以便通过身份验证。
代码语言:ruby
复制
describe "GET index" do
  it "returns http success" do
    authorize "username", "password"
    get :index
    expect(response).to have_http_status(:success)
  end
end
  1. 使用 ActionController::TestCase 中的 bypass_rescue 方法,以便在测试中跳过身份验证。
代码语言:ruby
复制
describe "GET index" do
  it "returns http success" do
    bypass_rescue
    get :index
    expect(response).to have_http_status(:success)
  end
end

这些方法可以帮助你在 RSpec 测试中跳过 Rails 中的 http_basic_authenticate_with 方法,从而更轻松地进行测试。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券