【问题标题】:Factory Bot Failures in Rails AppRails 应用程序中的工厂机器人故障
【发布时间】: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。您应该在location factory 中将其设置为regionassociation :region

标签: ruby-on-rails factory-bot


【解决方案1】:

您可以使用after_createfactory-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

【讨论】:

    猜你喜欢
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多