如何使 class 对 RSpec 测试文件可见?
How to make a class visible for RSpec test file?
我有模型Player,它位于app/models:
class Player < ApplicationRecord
...
end
我正在使用 gem rspec-rails
并且我有一个文件,其中包含一个测试文件,用于确定播放器是否已成功创建,位于 test/models:
require 'rspec'
require_relative '../../app/models/player.rb'
RSpec.describe 'Player' do
it 'saves new player' do
expect(Player.new.save).to eq true
end
end
我收到错误 NameError:未初始化的常量 ApplicationRecord。这可能是因为测试 类 没有看到模型 类。如何使它们可见并避免错误,从而可以测试新播放器的创建?
将 require 'rails_helper'
添加到文件顶部的规范中。您可以删除所有其他“要求”。
文档:https://relishapp.com/rspec/rspec-rails/v/4-0/docs/model-specs
我有模型Player,它位于app/models:
class Player < ApplicationRecord
...
end
我正在使用 gem rspec-rails
并且我有一个文件,其中包含一个测试文件,用于确定播放器是否已成功创建,位于 test/models:
require 'rspec'
require_relative '../../app/models/player.rb'
RSpec.describe 'Player' do
it 'saves new player' do
expect(Player.new.save).to eq true
end
end
我收到错误 NameError:未初始化的常量 ApplicationRecord。这可能是因为测试 类 没有看到模型 类。如何使它们可见并避免错误,从而可以测试新播放器的创建?
将 require 'rails_helper'
添加到文件顶部的规范中。您可以删除所有其他“要求”。
文档:https://relishapp.com/rspec/rspec-rails/v/4-0/docs/model-specs