【发布时间】:2018-03-09 20:22:39
【问题描述】:
所以我有以下模型:区域和位置。它们看起来很好,我可以很好地加载页面。问题与工厂机器人有关。
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
这两个都非常容易呈现,我对它们所做的唯一更改是将双引号换成单引号。但是当我运行 bin/test 时,我不断收到以下错误: Admin::LocationsController show should render the show page Failure/Error: location = FactoryBot.create(:location) ActiveRecord::RecordInvalid:Validation failed: Region must exist
然后是失败的协调示例:rspec ./spec/controllers/admin/locations_controller_spec.rb:16 # Admin::LocationsController show 应该呈现显示页面
所以我查看了 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
它引用的行是:它“应该渲染显示页面”。
老实说,我不知道是什么导致它失败。我认为这可能是一个关联问题,但是当我尝试执行关联之类的操作时:location 它导致填充更多错误。我只是在关联的规范控制器中遗漏了一些东西吗?
编辑/更新:使用关联 :region in location 修复了六个错误中的五个。但我得到:失败/错误:期望发布:创建,参数:{位置:FactoryBot.attributes_for(:location)} end.to change(Location, :count).by(1) 预期 #count 已更改加了 1,但是改了 0。
【问题讨论】:
-
错误很明显
ActiveRecord::RecordInvalid:Validation failed: Region must exist您在模型中强制执行非空区域,并且您在工厂中设置为nil。您应该在locationfactory 中将其设置为region或association :region