-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FactoryBot::InvalidFactoryError: When assigning attributes, you must pass a hash as an argument #44
Comments
Hi @joshRpowell! Thanks for the report, I'll do my best to take a look at the issue later this week |
@joshRpowell I was able to reproduce it! The problem is that in schema you set up
Here is a full example (save it to file, run begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
gem "rails"
gem "pg"
gem "factory_bot_rails"
gem "store_model"
end
require "active_record"
require "action_controller/railtie"
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "railstestdb")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
enable_extension "plpgsql"
create_table "worker_options", id: :serial, force: :cascade do |t|
t.jsonb "options", default: "{}", null: false
t.jsonb "data", default: "{}", null: false
# Option 1:
# t.jsonb "options", default: {}, null: false
# t.jsonb "data", default: {}, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
class WoCases
include StoreModel::Model
attribute :uri, :string
end
class Options
include StoreModel::Model
attribute :id, :integer
attribute :number, :string
attribute :receipt_number, :string
end
class WorkerOption < ActiveRecord::Base
attribute :data, WoCases.to_type
attribute :options, Options.to_type
# Option 2:
# attribute :data, WoCases.to_type, default: {}
# attribute :options, Options.to_type, default: {}
end
FactoryBot.define do
factory :worker_option, class: 'WorkerOption' do
data { { "uri" => 'http://localhost:3000/api/v1/work_flows/1/case_machines/1/cases' } }
trait :valid_options do
options { { "id" => 666, "number" => "NUMBER", "receipt_number" => "RECEIPT_NUMBER" } }
end
end
end
puts FactoryBot.create(:worker_option).inspect
# The following line always works, because all the attributes are set up!
puts FactoryBot.create(:worker_option, :valid_options).inspect |
I'm closing this, please let me know if you need any help here, and I'll reopen it 🙂 |
@DmitryTsepelev Thanks for the prompt response. I made the recommended adjustments and it resolved my issues. |
Thanks for the great gem! First time user.
Implementation works well except when I run my specs I get the error noted above after switching my attributes from :jsonb to
store_model
. Nothing complex about the app/implementation.Any thoughts on how to best handle?
schema.rb
running
rspec
/spec/support/init/factory_bot_rails.rb
factories/worker_option.rb
The text was updated successfully, but these errors were encountered: