尝试在 Rails 中设置 rspec 测试时获取未初始化的常量 Faker::String
Getting uninitialized constant Faker::String when attempting to set up rspec test in Rails
我有一个 UrlContent 模型,我正在尝试为 API 调用 Json 的索引页编写 rspec 测试。但是,每当我 运行 rspec 时,它都会给我这个错误:
Failure/Error: content { Faker::String }
NameError:
uninitialized constant Faker::String
当该属性的 faker 似乎已经正确配置时,为什么会出现该错误?
这让我感到困惑,因为在我的 spec/factories/url_content.rb 文件中我有:
需要'faker'
FactoryBot.define do
factory :url_content do
content { Faker::String }
end
end
我设置的具体 rspec 测试是:
require 'rails_helper'
require 'faker'
RSpec.describe "UrlContents API", type: :request do
before { FactoryBot.create_list(:url_content, 3) }
describe 'GET /url_contents' do
before { get '/url_contents'}
it 'returns a status code of 200' do
expect(response).to have_http_status(200)
end
it 'returns an array of content in JSON' do
json = JSON.parse(response.body, symbolize_names: true)
end
end
end
Faker::String
was added to the ruby gem 9 days ago.
The most recent gem version,在撰写本文时,是 1.8.7
;于 12 月 22 日发布(即在添加 Faker::String
之前)。
如果你想使用 gem 的那个特性,你需要告诉 bundler 使用 repo 的 master 分支:
gem 'faker', git: 'https://github.com/stympy/faker'
...或者,从源代码在本地构建 gem。
git clone git@github.com:stympy/faker.git
rake install
您选择在积极维护的 gem 上使用未发布的功能真是太不幸了。
但是,通常您可以检查 changelog
以查看您的 gem 版本是否足够新;或者,如果失败,则直接 git 历史记录。
您还可以 运行 bundle open faker
在您的项目中,浏览源代码以查看您的功能是否存在于已安装的 gem。
Faker::String
于 9 天前在 December 24 and the latest version of faker(1.8.7) was published on December 22 添加到伪造者 gem。您必须使用 Github 的版本才能使用 Faker::String
。在您的 Gemfile
中指定 git 存储库,如下所示:
gem 'faker', git: 'https://github.com/stympy/faker'
我有一个 UrlContent 模型,我正在尝试为 API 调用 Json 的索引页编写 rspec 测试。但是,每当我 运行 rspec 时,它都会给我这个错误:
Failure/Error: content { Faker::String }
NameError:
uninitialized constant Faker::String
当该属性的 faker 似乎已经正确配置时,为什么会出现该错误?
这让我感到困惑,因为在我的 spec/factories/url_content.rb 文件中我有:
需要'faker'
FactoryBot.define do
factory :url_content do
content { Faker::String }
end
end
我设置的具体 rspec 测试是:
require 'rails_helper'
require 'faker'
RSpec.describe "UrlContents API", type: :request do
before { FactoryBot.create_list(:url_content, 3) }
describe 'GET /url_contents' do
before { get '/url_contents'}
it 'returns a status code of 200' do
expect(response).to have_http_status(200)
end
it 'returns an array of content in JSON' do
json = JSON.parse(response.body, symbolize_names: true)
end
end
end
Faker::String
was added to the ruby gem 9 days ago.
The most recent gem version,在撰写本文时,是 1.8.7
;于 12 月 22 日发布(即在添加 Faker::String
之前)。
如果你想使用 gem 的那个特性,你需要告诉 bundler 使用 repo 的 master 分支:
gem 'faker', git: 'https://github.com/stympy/faker'
...或者,从源代码在本地构建 gem。
git clone git@github.com:stympy/faker.git
rake install
您选择在积极维护的 gem 上使用未发布的功能真是太不幸了。
但是,通常您可以检查 changelog
以查看您的 gem 版本是否足够新;或者,如果失败,则直接 git 历史记录。
您还可以 运行 bundle open faker
在您的项目中,浏览源代码以查看您的功能是否存在于已安装的 gem。
Faker::String
于 9 天前在 December 24 and the latest version of faker(1.8.7) was published on December 22 添加到伪造者 gem。您必须使用 Github 的版本才能使用 Faker::String
。在您的 Gemfile
中指定 git 存储库,如下所示:
gem 'faker', git: 'https://github.com/stympy/faker'