所以我有以下模型:区域和位置。它们看起来很好,我可以加载页面就好了。这个问题来自工厂机器人。
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应该呈现显示页面失败/错误:location = FactoryBot.create(:location)ActiveRecord :: RecordInvalid:验证失败:区域必须存在< / p>
然后是失败的协调示例: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
它引用的行是:'它应该呈现显示页'。
老实说,我不知道是什么导致它失败。我认为这可能是一个关联问题但是当我去尝试做类似关联的事情时:它会导致更多错误填充。我只是在规范控制器中缺少一些关联的东西吗?
编辑/更新:使用关联修复了六个错误中的五个:位置区域。但我得到:失败/错误:期望发帖:create,params:{location:FactoryBot.attributes_for(:location)} end.to change(Location,:count).by(1)期望#count已更改由1改变,但由0改变。
答案 0 :(得分:0)
您可以使用factory-bot
在after_create
中创建关联,试试这个
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
,请参阅此处