【发布时间】:2018-01-02 22:12:22
【问题描述】:
我有一个 UrlContent 模型,我正在尝试为 Json 的索引页面的 API 调用编写一个 rspec 测试。但是,每当我运行 rspec 时,它都会给我这个错误:
Failure/Error: content { Faker::String }
NameError:
uninitialized constant Faker::String
当属性的 faker 似乎已经正确配置时,为什么会给出该错误?
这让我很困惑,因为在我的 spec/factories/url_content.rb 文件中我有:
需要“伪装者”
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
【问题讨论】:
标签: ruby-on-rails ruby rspec factory-bot