【发布时间】:2021-08-17 12:06:39
【问题描述】:
我有如下测试环境配置
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
require 'minitest/rails'
# Consider setting MT_NO_EXPECTATIONS to not add expectations to Object.
# ENV["MT_NO_EXPECTATIONS"] = true
Dir[Rails.root.join('test/support/**/*.rb')].each { |f| require f }
class ActiveSupport::TestCase
# Run tests in parallel with specified workers
# parallelize(workers: :number_of_processors)
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
以及下面的测试
describe Abi::OrganizationsController do
before do
@request.headers['x-abi-token'] = 'test'
end
describe '#create' do
let(:credit_rating) { 'A' }
let(:organization_data) do
{
organization: {
name: 'Test Organization',
....
end
it 'should return an organization id' do
post(abi_organizations_path, params: organization_data)
end
end
end
在需要发送新标头之前,测试工作正常。
我尝试使用 @request.headers,但此时,@request 是 nil,我尝试过的没有任何实际帮助。
当我将课程更新为此时,我设法获得了 @request 值
class Abi::OrganizationsControllerTest < ActionController::TestCase
但是,我不得不把话题改成这样:
post :create, params: organization_data # <-- new subject
# post(abi_organizations_path, params: organization_data)
这是我不想做的事情,然后我开始看到这个错误
RuntimeError Exception: @controller is nil: make sure you set it in your test's setup method.
我有点迷路了,我在互联网上没有找到任何有用的东西,我觉得我是唯一一个遇到这个没有任何意义的问题的人。
以前有人试过吗?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-6 minitest