我当前正在将我们的一个项目中的Rails版本从5.0.7升级到5.1.7,但是我似乎无法找到关于为什么以前工作的调用BusinessTypes::ActiveRecord_Relation
为何没有解释的原因工作了。此调用是作为规范的一部分执行的,以便检查控制器是否使用正确的参数(以及具有正确类的ActiveRecord::Relation
)正确初始化了视图模型。
show-source BusinessTypes::ActiveRecord_Relation
BusinessTypes::ActiveRecord_Relation.source_location
ActiveRecord_Relation
子类,但没有找到任何东西,只有ActiveRecord::Relation
类本身,并不能真正解决我的问题。经过测试的方法BusinessTypes#index
如下:
def index
@vm = BusinessTypes::Index.new(@business_types.all, current_user, address_confirmed: address_confirmed?)
end
错误的规格如下:
let(:user) { create(:user) }
describe 'GET #index' do
it 'initialises the view model with the correct arguments' do
expect(BusinessTypes::Index).to receive(:new)
.with(kind_of(BusinessType::ActiveRecord_Relation), user, address_confirmed: true)
get :index
end
context 'user has not confirmed his address' do
let(:user) { create(:user, address_confirmed_at: nil) }
it 'initialises the view model with the correct arguments' do
expect(BusinessTypes::Index).to receive(:new)
.with(kind_of(BusinessType::ActiveRecord_Relation), user, address_confirmed: false)
get :index
end
end
end
该测试将通过,因为kind_of(BusinessTypes::ActiveRecord_Relation)
将返回对象#<RSpec::Mocks::ArgumentMatchers::KindOf:0x00007fe52529ae20 @klass=BusinessType::ActiveRecord_Relation>
,该对象与控制器作为参数传递的对象一致。
我收到以下错误:NameError: uninitialized constant BusinessTypes::ActiveRecord_Relation
两种情况之间唯一发生变化的是Rails版本。我监督过什么吗?谢谢您的时间!
答案 0 :(得分:0)
问题在于ActiveRecord_Relation
被定义为BusinessType
内部的私有常量。您不能在BusinessType
之外引用它。
https://www.rubydoc.info/stdlib/core/Module:private_constant
但是您可以像这样引用它:
BusinessTypes.const_get("ActiveRecord_Relation")
但是我不会以这种方式在测试中引用它。我认为更好的方法是:
expect(BusinessTypes::Index).to receive(:new).with(kind_of(BusinessType.all.class), # ...