Rails 应用中的 Factory Bot 失败
Factory Bot Failures in Rails App
所以我有以下模型:区域和位置。它们看起来不错,我可以很好地加载页面。问题出在工厂机器人身上。
class Region < ApplicationRecord
scope :ordered, -> { order(name: :desc) }
scope :search, ->(term) { where('name LIKE ?', "%#{term}%") }
has_many :locations, dependent: :destroy
end
class Location < ApplicationRecord
scope :ordered, -> { order(name: :desc) }
scope :search, ->(term) { where('name LIKE ?', "%#{term}%") }
belongs_to :region
end
对于我的工厂,我有以下内容:
FactoryBot.define do
factory :region do
name 'MyString'
end
end
FactoryBot.define do
factory :location do
name 'MyString'
hours_operation 'MyString'
abbreviation 'MyString'
region nil
end
end
这两个都非常容易呈现,我对它们所做的唯一更改是将双引号换成单引号。但是当我 运行 a bin/test 我不断收到以下错误: Admin::LocationsController show should render the show page Failure/Error: location = FactoryBot.create(:location) ActiveRecord::RecordInvalid:Validation失败:区域必须存在
然后是失败的协调示例:rspec ./spec/controllers/admin/locations_controller_spec.rb:16 # Admin::LocationsController show should render the show page
所以我查看了 spec/controllers/admin/locations_controller_spec.rb,这是代码:
require 'rails_helper'
RSpec.describe Admin::LocationsController, type: :controller do
before(:each) do
activate_session(admin: true)
end
describe 'index' do
it 'should render the index' do
get :index
expect(response).to have_http_status :success
end
end
describe 'show' do
it 'should render the show page' do
location = FactoryBot.create(:location)
get :show, params: { id: location.id }
expect(response).to have_http_status :success
end
it 'should return a 404 error' do
get :show, params: { id: 1 }
expect(response).to have_http_status :not_found
end
end
describe 'new' do
it 'should render the new page' do
get :new
expect(response).to have_http_status :success
end
end
describe 'edit' do
it 'should render the edit page' do
location = FactoryBot.create(:location)
get :edit, params: { id: location.id }
expect(response).to have_http_status :success
end
end
describe 'create' do
it 'should create the record' do
expect do
post :create, params: { location:
FactoryBot.attributes_for(:location) }
end.to change(Location, :count).by(1)
end
end
describe 'update' do
it 'should update the record' do
location = FactoryBot.create(:location)
current_value = location.name
new_value = current_value + '-Updated'
expect do
patch :update, params: { id: location.id, location: { name: new_value } }
location.reload
end.to change(location, :name).to(new_value)
end
end
describe 'destroy' do
it 'should destroy the record' do
location = FactoryBot.create(:location)
expect do
delete :destroy, params: { id: location.id }
end.to change(Location, :count).by(-1)
end
end
end
它引用的行是:'should render the show page'.
老实说,我不知道是什么导致它失败。我认为这可能是一个关联问题,但是当我尝试做一些类似 associations :location 的事情时,它导致了更多的错误。我只是在关联的规范控制器中遗漏了一些东西吗?
EDIT/UPDATE:修复了在位置中使用关联 :region 的六个错误中的五个。但我得到: Failure/Error: expect do post :create, params: { location: FactoryBot.attributes_for(:location) } end.to change(Location, :count). by(1) 预计 #count 已更改为 1,但已更改为 0。
您可以使用 after_create
在 factory-bot
中创建关联,试试这个
FactoryBot.define do
factory :location do
name 'MyString'
hours_operation 'MyString'
abbreviation 'MyString'
after(:create) do |location, evaluator|
location.region = FactoryBot.create(:region)
end
end
结束
这一切所做的就是创建一个 region
对象并将其关联到 location
对象。
查看此处获取更详细的指南Factory Bot Guides
所以我有以下模型:区域和位置。它们看起来不错,我可以很好地加载页面。问题出在工厂机器人身上。
class Region < ApplicationRecord
scope :ordered, -> { order(name: :desc) }
scope :search, ->(term) { where('name LIKE ?', "%#{term}%") }
has_many :locations, dependent: :destroy
end
class Location < ApplicationRecord
scope :ordered, -> { order(name: :desc) }
scope :search, ->(term) { where('name LIKE ?', "%#{term}%") }
belongs_to :region
end
对于我的工厂,我有以下内容:
FactoryBot.define do
factory :region do
name 'MyString'
end
end
FactoryBot.define do
factory :location do
name 'MyString'
hours_operation 'MyString'
abbreviation 'MyString'
region nil
end
end
这两个都非常容易呈现,我对它们所做的唯一更改是将双引号换成单引号。但是当我 运行 a bin/test 我不断收到以下错误: Admin::LocationsController show should render the show page Failure/Error: location = FactoryBot.create(:location) ActiveRecord::RecordInvalid:Validation失败:区域必须存在
然后是失败的协调示例:rspec ./spec/controllers/admin/locations_controller_spec.rb:16 # Admin::LocationsController show should render the show page
所以我查看了 spec/controllers/admin/locations_controller_spec.rb,这是代码:
require 'rails_helper'
RSpec.describe Admin::LocationsController, type: :controller do
before(:each) do
activate_session(admin: true)
end
describe 'index' do
it 'should render the index' do
get :index
expect(response).to have_http_status :success
end
end
describe 'show' do
it 'should render the show page' do
location = FactoryBot.create(:location)
get :show, params: { id: location.id }
expect(response).to have_http_status :success
end
it 'should return a 404 error' do
get :show, params: { id: 1 }
expect(response).to have_http_status :not_found
end
end
describe 'new' do
it 'should render the new page' do
get :new
expect(response).to have_http_status :success
end
end
describe 'edit' do
it 'should render the edit page' do
location = FactoryBot.create(:location)
get :edit, params: { id: location.id }
expect(response).to have_http_status :success
end
end
describe 'create' do
it 'should create the record' do
expect do
post :create, params: { location:
FactoryBot.attributes_for(:location) }
end.to change(Location, :count).by(1)
end
end
describe 'update' do
it 'should update the record' do
location = FactoryBot.create(:location)
current_value = location.name
new_value = current_value + '-Updated'
expect do
patch :update, params: { id: location.id, location: { name: new_value } }
location.reload
end.to change(location, :name).to(new_value)
end
end
describe 'destroy' do
it 'should destroy the record' do
location = FactoryBot.create(:location)
expect do
delete :destroy, params: { id: location.id }
end.to change(Location, :count).by(-1)
end
end
end
它引用的行是:'should render the show page'.
老实说,我不知道是什么导致它失败。我认为这可能是一个关联问题,但是当我尝试做一些类似 associations :location 的事情时,它导致了更多的错误。我只是在关联的规范控制器中遗漏了一些东西吗?
EDIT/UPDATE:修复了在位置中使用关联 :region 的六个错误中的五个。但我得到: Failure/Error: expect do post :create, params: { location: FactoryBot.attributes_for(:location) } end.to change(Location, :count). by(1) 预计 #count 已更改为 1,但已更改为 0。
您可以使用 after_create
在 factory-bot
中创建关联,试试这个
FactoryBot.define do
factory :location do
name 'MyString'
hours_operation 'MyString'
abbreviation 'MyString'
after(:create) do |location, evaluator|
location.region = FactoryBot.create(:region)
end
end
结束
这一切所做的就是创建一个 region
对象并将其关联到 location
对象。
查看此处获取更详细的指南Factory Bot Guides