paulelliott / fabrication Goto Github PK
View Code? Open in Web Editor NEWThis project has moved to GitLab! Please check there for the latest updates.
Home Page: https://gitlab.com/fabrication-gem/fabrication
License: MIT License
This project has moved to GitLab! Please check there for the latest updates.
Home Page: https://gitlab.com/fabrication-gem/fabrication
License: MIT License
Under both 0.9.5 and 1.0.0, if I define a fabricator for a Sequel model and then use it, it creates an instance of the model but does not save it to the database. If I manually call save on the instantiated object right after it's created, then it persists just fine.
The issue is this line:
instance.save! if options[:save] && instance.respond_to?(:save!)
https://github.com/paulelliott/fabrication/blob/master/lib/fabrication/generator/base.rb#L68
Sequel models don't have #save! methods, just #save, so nothing happens when it's time to persist them. I tried simply dropping the exclamation points, like so:
instance.save if options[:save] && instance.respond_to?(:save)
This works properly for Sequel models, but obviously would be aggravating for users of other ORMs. I'm not sure what the best way to fix this would be.
Thanks!
# some basic AR models
class Person < ActiveRecord::Base ; end
class KeyPerson < Person ; end
# very simple fabricators
Fabricator(:person) { name 'Pablo' }
Fabricator(:key_person, :from => :person, :class_name, :key_person) {}
Let's assume every fabricator is in a different file. If person is loaded first and then key_person, it work as expected. If key_person is loaded first, it generate an empty object.
This kind of scenario should at least generate a warning like 'person fabricator is missing' or something like that.
May be a better solution will be to lazy build fabricators after load them.
See Gist: http://gist.github.com/609173
Named scopes dont play nice with the association syntax.
I noticed today that after_create
callbacks are executed even when building a new object. This is unexpected as an object hasn't been created, but rather only initialized.
how does has_one work? There are no samples in the README.
Hi,
I use namespaced models a lot, but everytime I create a new namespaced model my tests begin to fail, as I must set :class_name for the created fabrications.
For example:
I create a model Project::Task, the fabrication looks like:
Fabrication(:task) do
end
That's bad, because Task might already be defined for something else or simply the class is not found. A solution would look like automatically generated) - it's what I am doing by hand right now:
Fabrication(:project_task, class_name: Project::Task) do
end
Haven't yet looked at the source of fabrication - is there any simple way to add this functionality?
As I am using mongoid, I am not sure if it's mongoid specific.
Anyway, great work so far! =9
Cheers, Gregor Robert
The following code (I have a model with type
field):
Fabricator(:interesting_post, :from => :post) do
type PostType::INTERESTING
end
raises exception on Ruby 1.8.6. At the same time there's no explicit info on which minimal ruby version is supported.
When using a workaround for date objects AND assignment on virtual attributes in Rails 2.x - 3.x
Bug / Workaround: https://rails.lighthouseapp.com/projects/8994/tickets/2675-support-for-multiparameter-attribute-assignment-on-virtual-attribute-writers
Details on what happens with Fabrication: http://gist.github.com/565728
Hey again.
I guess the fault is on my side but I don't know where else to ask:
Here is the code:
Fabricator(:ad) do
# If I use Fabricate.sequence(:ad) { |i| "test_ad_#{i}" }
# on the rails console it works perfectly.
# Using it in a fabricator like below does not unfortunately.
name { sequence(:ads) { |i| "test_ad_#{i}" } }
end
When using Fabricate(:ad) two times on the console it'll throw an error:
ActiveRecord::RecordInvalid: Database entry invalid: Name is already taken
PS: Are there any other places to ask for help than via Github issues?
When running bundle exec rspec spec/models/something.rb
Fabricator will create the object when calling Fabricate(:something)
but it does not fill in the attributes for some reason.
It does work fine when running rake spec:models
though.
Have this error in generator "undefined Fabrication" (rails 3.0.7, fabrication 0.9.5)
Fixed by downgrade to version 0.9.3
Problem in file lib/rails/generators/fabrication/model/model_generator.rb
in 0.9.3
module Fabrication
module Generators
in 0.9.5
module Fabrication::Generators
(Note: The reason I'm making the issue here is because Mongoid works normally according to my console testing.)
My Fabrication for Account:
Fabricator(:account) do
email { Forgery(:internet).email_address }
password { Forgery(:basic).password(:at_least => 8, :at_most => 256) }
name { Forgery(:name).full_name }
age { Forgery(:basic).number(:at_least => 13, :at_most => 80) }
terms true
links :count => 10
end
My Links fabrication:
Fabricator(:link) do
title { Forgery(:lorem_ipsum).words(rand(7) + 2) }
url {
domain = Forgery(:internet).domain_name
path = Forgery(:lorem_ipsum).words(rand(3) + 1).gsub(/\s/,'/')
extention = ["index.html", "file.pdf", ""].random
"http://#{domain}/#{path}/#{extention}"}
end
Creating these objects works without problem:
:014 > Fabricate(:account)
=> #<Account _id: 4e62fcb1a53b22767600000c, _type: "Account", created_at: 2011-09-04 04:21:05 UTC, updated_at: 2011-09-04 04:21:05 UTC, deleted_at: nil, version: 1, email: "[email protected]", password: "$2a$10$MiISQ5y4eZLjqjChexdi2ewQurYcdnCDb.P.b4SD0UtBWlW5jDCpW", name: "Carolyn Edwards", age: 61, admin: false, support: false, terms: true, banned: false, disabled: false>
:016 > Fabricate(:link)
=> #<Link _id: 4e62fceea53b227676000017, _type: "Link", created_at: 2011-09-04 04:22:06 UTC, updated_at: 2011-09-04 04:22:06 UTC, deleted_at: nil, version: 1, title: "lorem ipsum", url: "http://avaveo.biz/lorem/file.pdf", dead: false, account_id: nil>
Creating these objects without Fabrication and associating them works without problem:
:017 > account = Account.new Fabricate.attributes_for :account
=> #<Account _id: 4e62fd21a53b227676000023, _type: "Account", created_at: nil, updated_at: nil, deleted_at: nil, version: 1, email: "[email protected]", password: "X0wvvXui3iLydnWCMxSP53sd3fZCwn1zVURYDXCKjFNwAOLHeoR0ZfEESXxTe7e3N9P3lzFtY0UdtLogYHUIBAAECb0Wy3mEnLJlB1nmAg1Xe8mwQfTaGgTQf4uccW87Sa48QcIo5hhxtqZ4jAlgPBTTsPiLQGzg7DQ6BHBFUNg6Iq2Lgnhy1yiqNeRz3nwlO1wLBQdfaLbpJAmrFnQNbF1nvE4YL4Akspr", name: "Harold Anderson", age: 13, admin: false, support: false, terms: true, banned: false, disabled: false>
:018 > link = Link.new Fabricate.attributes_for :link
=> #<Link _id: 4e62fd31a53b227676000025, _type: "Link", created_at: nil, updated_at: nil, deleted_at: nil, version: 1, title: "lorem ipsum dolor sit amet consectetuer adipiscing", url: "http://yotz.net/lorem/", dead: false, account_id: nil>
:019 > account.links << link
=> [#<Link _id: 4e62fd31a53b227676000025, _type: "Link", created_at: nil, updated_at: nil, deleted_at: nil, version: 1, title: "lorem ipsum dolor sit amet consectetuer adipiscing", url: "http://yotz.net/lorem/", dead: false, account_id: BSON::ObjectId('4e62fd21a53b227676000023')>]
:020 > account.links
=> [#<Link _id: 4e62fd31a53b227676000025, _type: "Link", created_at: nil, updated_at: nil, deleted_at: nil, version: 1, title: "lorem ipsum dolor sit amet consectetuer adipiscing", url: "http://yotz.net/lorem/", dead: false, account_id: BSON::ObjectId('4e62fd21a53b227676000023')>]
:021 > account.save
=> true
:022 > link.save
=> true
:023 > account.reload
=> #<Account _id: 4e62fd21a53b227676000023, _type: "Account", created_at: 2011-09-04 04:23:31 UTC, updated_at: 2011-09-04 04:23:31 UTC, deleted_at: nil, version: 1, email: "[email protected]", password: "$2a$10$C8Q8gHi2c4hR3QwW86pSPuGWGr7ZcF70n9X0ZZGUrTNGUCyPEsf9O", name: "Harold Anderson", age: 13, admin: false, support: false, terms: true, banned: false, disabled: false>
:024 > account.links
=> [#<Link _id: 4e62fd31a53b227676000025, _type: "Link", created_at: 2011-09-04 04:23:34 UTC, updated_at: 2011-09-04 04:23:34 UTC, deleted_at: nil, version: 1, title: "lorem ipsum dolor sit amet consectetuer adipiscing", url: "http://yotz.net/lorem/", dead: false, account_id: BSON::ObjectId('4e62fd21a53b227676000023')>]
And now to check relations with the Fabrication creation:
:027 > Account.count
=> 0
:028 > Link.count
=> 0
:029 > account = Fabricate(:account)
=> #<Account _id: 4e62fd96a53b227676000026, _type: "Account", created_at: 2011-09-04 04:24:54 UTC, updated_at: 2011-09-04 04:24:54 UTC, deleted_at: nil, version: 1, email: "[email protected]", password: "$2a$10$qr.IcMyXes2s4wr70BOrPeEUdAXtL17.6DB9AvZAAU/LvPstAHGFC", name: "Ruby Mcdonald", age: 20, admin: false, support: false, terms: true, banned: false, disabled: false>
:030 > account.links
=> []
:031 > account.links.first
=> #<Link _id: 4e62fd96a53b227676000027, _type: "Link", created_at: 2011-09-04 04:24:54 UTC, updated_at: 2011-09-04 04:24:54 UTC, deleted_at: nil, version: 1, title: "lorem ipsum", url: "http://zoomlounge.edu/lorem/file.pdf", dead: false, account_id: BSON::ObjectId('4e62fd96a53b227676000026')>
:032 > account.links.last
=> #<Link _id: 4e62fd96a53b227676000030, _type: "Link", created_at: 2011-09-04 04:24:54 UTC, updated_at: 2011-09-04 04:24:54 UTC, deleted_at: nil, version: 1, title: "lorem ipsum dolor sit amet consectetuer adipiscing", url: "http://feednation.org/lorem/ipsum/dolor/", dead: false, account_id: BSON::ObjectId('4e62fd96a53b227676000026')>
:033 > Account.count
=> 1
:034 > Link.count
=> 10
How strange is that?
Sequel support would be really awesome.
The following is the associations for my app model:
class Gallery < ActiveRecord::Base
belongs_to :galleried, :polymorphic => true
has_many :photos, :dependent => :destroy
end
class Photo < ActiveRecord::Base
belongs_to :gallery
end
class Event < ActiveRecord::Base
has_one :gallery, :as => :galleried, :dependent => :destroy
end
class Place < ActiveRecord::Base
has_one :gallery, :as => :galleried, :dependent => :destroy
end
And the fabrications fabricators.rb
Fabricator(:gallery) do
galleried!{Fabricate(:place)}
end
Fabricator(:gallery_place, :from => :gallery) do
galleried!{Fabricate(:place)}
end
Fabricator :photo do
gallery!
end
The problem is with the fabricator for gallery. I'd to use Fabricate(:place)
or Fabricate(:event)
which can't be done coz its polymorphic model.
Fabricator(:gallery) do
galleried!{Fabricate(:place)}
end
How can I handle this?
Hi,
Then follow my model declarations:
class Association < ActiveRecord::Base
attr_accessible :title
has_many :members, :foreign_key => "asso_id", :dependent => :destroy
after_create :create_member
private
def create_member
self.members.create!({:name => "MyRealName"})
end
end
class Member < ActiveRecord::Base
attr_accessible :name
belongs_to :asso, :class_name => "Association", :dependent => :delete
end
And the fabricators :
Fabricator(:association) do
title "MyAsso"
after_create do |asso|
Fabricate(:member, :asso => asso)
end
end
Fabricator(:member) do
name "MyName"
asso { Fabricate(:association) }
end
I surely missed something, but here are the Rspec issues I have:
Failure/Error: let(:asso) { Fabricate(:association) }
ActiveRecord::UnknownAttributeError:
unknown attribute: asso_id
Hope somebody can help me ;)
Cheers, Alex.
I'm using Fabrication with Mongoid under Rails 3.1 rc4.
say we have
class Parent
embeds_many :children
end
class Child
embedded_in :parent
embeds_many :grand_children
end
class GrandChild
embedded_in :child
end
Fabricator(:parent) do
end
Fabricator(:child) do
parent
end
Fabricator(:grand_child) do
child
end
Then call
Fabricate(:grand_child)
will fail to fabricate the object.
I'm wondering if there is a method or a trick of doing this using Fabrication...?
I implemented this in my project via a few small monkey patches, and found it extremely useful.
Fabricate.unique(:some_fabricator)
The idea here is it will save each record with a fabricator_id
field, and if that fabricator_id already exists, it'll just return it instead of creating.
It was only a few lines needed to get working with Mongoid. I figure it'll be trickier though with a relational db because you'll need to add the fabricator_id to the schema.
Might be a cool feature to implement on fabrication gem at some point.
Calling a sequence by name should apply the block it was created with.
Fabricate.sequence :ord, &:ordinalize
=> "0th"
Fabricate.sequence :ord
=> "1st"
/home/mike/.rvm/gems/ruby-head/gems/fabrication-1.0.1/lib/rails/generators/fabrication/cucumber_steps/cucumber_steps_generator.rb:3:in `<top (required)>': uninitialized constant Fabrication (NameError)
from /home/mike/.rvm/gems/ruby-head/gems/railties-3.1.0.rc5/lib/rails/generators.rb:297:in `block (2 levels) in lookup'
from /home/mike/.rvm/gems/ruby-head/gems/railties-3.1.0.rc5/lib/rails/generators.rb:293:in `each'
from /home/mike/.rvm/gems/ruby-head/gems/railties-3.1.0.rc5/lib/rails/generators.rb:293:in `block in lookup'
from /home/mike/.rvm/gems/ruby-head/gems/railties-3.1.0.rc5/lib/rails/generators.rb:292:in `each'
from /home/mike/.rvm/gems/ruby-head/gems/railties-3.1.0.rc5/lib/rails/generators.rb:292:in `lookup'
from /home/mike/.rvm/gems/ruby-head/gems/railties-3.1.0.rc5/lib/rails/generators.rb:149:in `find_by_namespace'
from /home/mike/.rvm/gems/ruby-head/gems/railties-3.1.0.rc5/lib/rails/generators.rb:166:in `invoke'
from /home/mike/.rvm/gems/ruby-head/gems/railties-3.1.0.rc5/lib/rails/commands/generate.rb:12:in `<top (required)>'
from /home/mike/.rvm/gems/ruby-head/gems/railties-3.1.0.rc5/lib/rails/commands.rb:28:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I just typed in 'rails generate fabrication:cucumber_steps' and got the error message you see above.
Fabricator :workday do
day 1
break 1
end
spits out an error that the Fabricator :workday
can't be found
Fabricator :workday do |f|
f.day { 1 }
f.break { 1 }
end
fixes it
Bug fix? Documentation? Maybe this is just bad naming on my part but maybe it isn't...
It was a yucky bug to track down. I switched to FactoryGirl and when it broke there too, I found 'er.
When developing using a tool like spork, it's very messy to reload the full rails stack to update the fabricator's definition, can you please add some kind of reloading mechanism?
I want to use Fabrication to create a Ruby object, such as a Hash.
Can you give me an example on how the Fabricator would looks like?
Thank you
I have 3 fabricators called flow, node and user in the spec/fabricators folder. However when I call Fabricate on any one of them, I get 2 kinds of errors.
and the slightly more puzzling
I'm not sure why it's trying to find a class called client. I do have a fabricator and a model named client, but this is happening when I do
ruby-1.9.2-p180 :001 > Fabricate(:node)
Node model does not have any relationship to client.
(I'm using version 1.0.1)
Example:
Fabricator :quiz do
user { Fabricate :user }
end
Fabricator :built_quiz, :from => :quiz do
after_create do |quiz|
quiz.build!
end
end
Fabricator :answered_quiz, :from => :built_quiz do
after_create do |quiz|
randomly_answer(quiz)
end
end
Fabricate :answered_quiz tries to answer an unbuilt quiz. It can be worked around, it's just unexpected.
Edit: Fixed formatting.
✗ ./script/rails g fabrication
Could not find generator fabrication.
Rails-3.0.1
Fabrication-0.9.0
Ruby EE 1.8.7-253
/Users/sebastian/Sites/rails/event-click/spec/models/event_spec.rb:5: invalid multibyte char (US-ASCII)
/Users/sebastian/Sites/rails/event-click/spec/models/event_spec.rb:5: syntax error, unexpected $end, expecting ')'
...ce, :place_name => 'Karlsruheö', :postal_code => '76227')
... ^
from /usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/gems/1.9.1/gems/rspec-core-2.0.0/lib/rspec/core/configuration.rb:306:in block in load_spec_files' from /usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/gems/1.9.1/gems/rspec-core-2.0.0/lib/rspec/core/configuration.rb:306:in
map'
from /usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/gems/1.9.1/gems/rspec-core-2.0.0/lib/rspec/core/configuration.rb:306:in load_spec_files' from /usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/gems/1.9.1/gems/rspec-core-2.0.0/lib/rspec/core/command_line.rb:18:in
run'
from /usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/gems/1.9.1/gems/rspec-core-2.0.0/lib/rspec/core/runner.rb:55:in run_in_process' from /usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/gems/1.9.1/gems/rspec-core-2.0.0/lib/rspec/core/runner.rb:46:in
run'
from /usr/local/Cellar/ruby/1.9.2-p0/lib/ruby/gems/1.9.1/gems/rspec-core-2.0.0/lib/rspec/core/runner.rb:10:in `block in autorun'
The call is:
karlsruhe = Fabricate(:place, :place_name => 'Karlsruheö', :postal_code => '76227')
Installed gems are:
This would clean up many of the issues Factory Girl, Machinist, etc run into.
The goal would be, try to create Fabricate.find_or_create(:database, :name => 'john') so that you do not have to worry about validation errors etc when building multiple fixtures. This is what you run into alot more when you do ! methods.
This can be extended to do find_or_build(:database,...) as well.
Otherwise loving fabrication :)
Update: see comment below - the validation issue described is a secondary symptom.
Hi,
I'm having problems where lazy-loaded associations that are the subject of a validates_presence_of
validation are failing to work - validation fails, and the association returns nil.
Basic example to reproduce (sorry - will try to make time to submit a failing test later):
# Models
class Network < ActiveRecord::Base; end
class Merchant < ActiveRecord::Base
belongs_to :network
validates_presence_of :network
end
# Fabricators
Fabricator(:network)
Fabricator(:merchant) { network }
# merchant_spec.rb
describe Merchant do
let(:merchant) { Fabricate(:merchant) }
it "is valid" do
merchant.should be_valid
end
it "has a network" do
merchant.network.should be_present
end
end
Both of these tests fail with the validation in place. If the validation is removed, they both pass.
I can see there's something fiendishly clever going on in Generator::ActiveRecord
to overwrite the association accessor. I can't, however, figure out why a) the #save!
call doesn't throw an exception (the instance is, after all, invalid by its own insistence), and why subsequent calls to the association accessor don't cause the stored proc to fire.
Bizarrely, when I did the following (to let me check that the accessor overwriting was actually happening):
class Merchant < ActiveRecord::Base
belongs_to :network
validates_presence_of :network
def network_with_debugger
network_without_debugger
debugger
end
alias_method_chain :network, :debugger
end
...then both tests pass even if I don't actually do anything in the debugger session. I can see the proc stored in @__network_block
, and call it without problems.
All very odd.
It currently just sets the attribute to an array, thusly breaking things like record.associations.build etc.
Bad bug report :(
[example]
rails g scaffold admin/team
[generate]
invoke active_record
create db/migrate/20110621120306_create_admin_teams.rb
create app/models/admin/team.rb
identical app/models/admin.rb
invoke rspec
create spec/models/admin/team_spec.rb
invoke fabrication
create spec/fabricators/admin_team_fabricator.rb
route namespace :admin do resources :teams end
invoke inherited_resources_controller
create app/controllers/admin/teams_controller.rb
invoke haml
create app/views/admin/teams
create app/views/admin/teams/index.html.haml
create app/views/admin/teams/edit.html.haml
create app/views/admin/teams/show.html.haml
create app/views/admin/teams/new.html.haml
create app/views/admin/teams/_form.html.haml
invoke rspec
create spec/controllers/admin/teams_controller_spec.rb
create spec/views/admin/teams/edit.html.haml_spec.rb
create spec/views/admin/teams/index.html.haml_spec.rb
create spec/views/admin/teams/new.html.haml_spec.rb
create spec/views/admin/teams/show.html.haml_spec.rb
invoke helper
create spec/helpers/admin/teams_helper_spec.rb
create spec/routing/admin/teams_routing_spec.rb
invoke rspec
create spec/requests/admin/admin_teams_spec.rb
invoke helper
create app/helpers/admin/teams_helper.rb
invoke rspec
invoke stylesheets
identical public/stylesheets/scaffold.css
[expect]
Fabricator(:team, :from=>:admin) do end
or
Fabricator(:"Admin::Team") do end
[disappointment]
Fabricator(:team) do end
I want save the trouble of modify ./spec/fabricators/<singular_table_name>_fabricator.rb
Getting a failure when I run the specs on master (gisted at http://gist.github.com/574536).
It occurs on both 1.8.7 and 1.9.2. If I comment out "before(:all) { Fabrication::Support.find_definitions }" (line 39 in spec/support_spec.rb) everything is green. I'd submit a pull request, but I'm not familiar enough with the codebase yet to know why it's set up the way it is.
with ActiveRecord, suppose I have a Book model and an Author model and
class Book < ActiveRecord::Base
has_many :authors, through: :books_authors
end
class Author < ActiveRecord::Base
has_many :books, through: :books_authors
end
And the requirement is that a book has to have at least an author, therefore in my book fabricator, I have
Fabricator(:book) do
authors! { Fabricate(:author) }
end
In my cucumber test, I want to be able to say
Given 1 book
And that book has the following author:
| name | John Doe |
.....
Currently if I do this the book will have two authors, one created with the book fabricator and John Doe. I'd like to be able to only have John Doe has the author for the book.
Fabricator complains that it can't load any fabrications files. This is due to how require changes in ruby 1.9.2. See this for more info: http://help.hoptoadapp.com/discussions/problems/778-nomethoderror-params_filters
A simple fix: require './spec/fabricators/foo.rb' instead of 'spec/fabricators/foo.rb'
I'd fork and fix it, but I don't have time to test it on all the different ruby versions. Sorry!
rails generate model foo
would be nice if it generated spec/fabrications/foo.rb
On http://fabricationgem.org/#!defining-fabricators
Fabricator(:person) do
vehicle { Fabricator(:vehicle) }
end
should be
Fabricator(:person) do
vehicle { Fabricate(:vehicle) }
end
I thought for a moment that it was magical new way to define a fabricator in place, but that approach yields something along the lines of:
Vehicle(#2189032200) expected, got Fabrication::Schematic(#2189689340) (ActiveRecord::AssociationTypeMismatch)
or a message about duplicate fabricator definitions, depending on whether you have a fabricator of that name defined.
I have following model in a rails(3.0.7):
class MyModel
include Mongoid::Document
field :type, :type => Integer
validates :type, :presence => true
end
and following fabricator:
Fabricator(:my_model) do
type { rand+1 }
end
and ruby:
# ruby -v
ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-linux], MBARI 0x8770, Ruby Enterprise Edition 2011.03
on project startup I get following warnings:
/home/kuca/project/fabricators/my_model_fabricator.rb:4: warning: Object#type is deprecated; use Object#class
/home/kuca/project/fabricators/my_model_fabricator.rb:10: warning: Object#type is deprecated; use Object#class
/home/kuca/project/fabricators/my_model_fabricator.rb:14: warning: Object#type is deprecated; use Object#class
/home/kuca/project/fabricators/my_model_fabricator.rb:18: warning: Object#type is deprecated; use Object#class
/home/kuca/project/fabricators/my_model_fabricator.rb:22: warning: Object#type is deprecated; use Object#class
/home/kuca/project/fabricators/my_model_fabricator.rb:26: warning: Object#type is deprecated; use Object#class
/home/kuca/project/fabricators/my_model_fabricator.rb:30: warning: Object#type is deprecated; use Object#class
/home/kuca/project/fabricators/my_model_fabricator.rb:34: warning: Object#type is deprecated; use Object#class
and of course value for MyModel#type
is not filled because in this case Fabrication::Schematic#type
returns Fabrication::Schematic
and do not run corresponding code from fabrication
this fixed the problem:
# config/initializers/fabrication.rb 16:08
# to get field named 'type' work in REE
if defined? Fabrication::Schematic
Object.send :undef_method, :type if RUBY_DESCRIPTION=~/Ruby Enterprise Edition/
end
but I guess You should now about this issue, and ,maybe fix it on fabrication level
Please see: http://pastie.org/1137371
Three things:
a = Fabricate.build :author
actually works, but attributes_for does not. Why?if you only create a single record with Fabrication for a has_many association, it does not wrap it inside an array.
Fix: addresses! { [Fabricate(:address)] }
I'm not sure if this is a bug or not. Factory Girl seems to behave the same way.
I have an invite Fabricator:
Fabricator :invite do
email { Faker::Internet.email }
user! { Fabricate :user }
end
a standard user fabricator. And I have this rspec test:
context Invite do
it "shouldn't create an extra user when creating an invite"
User.count.should == 0
user = Fabricate :user
invite = Fabricate :invite, :user => user
invite.user.id.should == user.id
User.count.should == 1
end
end
However, the last line fails with User.count of 2
Why is it creating the extra user? Can I get around this?
Should be fairly easy to replicate with a non-trivial model (one with at least one association defined). Let me know if you want examples.
Thanks,
Obie
This is a really weird bug, it's pretty hard to describe:
This is the fabricator I have.
Fabricator(:user) do
name Faker::Name.name
email Faker::Internet.email
password 'secret'
zip_code Faker::Address.zip_code
end
If I go into Rails console and call it, I get a new User. If I call it again, it seems to not create a new user, but return the same user that was just created, except certain fields aren't set, like created_at or updated_at. It doesn't matter how many times I fabricate a user, only one is ever created. Any idea what's wrong? I'm running Rail3 with mongoid on 1.9.2.
Fabricated objects that are cloned (using #clone
) don't behave the same as AR model objects that are cloned. For instance, a clone of a fabricated model appears to clone properly, but validations are performed on the original fabricated model, and not the clone instance.
To illustrate:
Given a User
AR model with required #name
and #email
attributes, a valid :user
definition and a valid User
record in the database with #id
of 1
:
user = User.find_by_id(1)
fabricated_user = Fabricate(:user)
cloned_user = user.clone
cloned_fab_user = fabricated_user.clone
# Set clone name to nil; clones should fail validation
cloned_user.name = nil
cloned_fab_user.name = nil
user.valid? # => true (as expected)
fabricated_user.valid? # => true (as expected)
cloned_user.valid? # => false (as expected)
cloned_fab_user.valid? # => true, but should be false
# Set clone name to non-blank value; clones should pass validation
# Set source object name to nil; should not affect clone validations
cloned_user.name = 'Albert'
cloned_fab_user.name = 'Albert'
user.name = nil
fabricated_user.name = nil
user.valid? # => false (as expected)
fabricated_user.valid? # => false (as expected)
cloned_user.valid? # => true (as expected)
cloned_fab_user.valid? # => false, but should be true
cloned_fab_user.errors # => { :name => ["can't be blank"] }, but should be empty Hash
# Set clone email to nil, source object name to nil
# Fabricated clone shows error message for source
cloned_fab_user.email = nil
fabricated_user.name = nil
cloned_fab_user.valid? # => false (as expected)
cloned_fab_user.errors # => { :name => ["can't be blank"] }, should be error for :email
I ran into this using Rails v3.0.10 and Fabrication v1.1.0.
I have a before_create hook that looks like this:
if address && address.new_record?
address.user = self.user
end
Unfortunately, the first part, if address
evaluates as true. If I remove the second part of the statement and do something like:
if address
puts "#{address.inspect} - #{address.class.inspect} - #{address.methods - ActiveRecord::Base).methods.inspect}"
end
... I get something along the lines of:
"nil - NilClass - [:to_i, :to_f, :to_a, :&, :|, :^, :to_r, :rationalize, :to_c]"
Which looks like it really is nil. Except the interpreter never should have gotten to that puts statement if address
really was nil. This is a new bug introduced when I switched from Machinist to Fabrication, so I'm guessing it's coming from this library.
I poked around the code, and my best guess is that the redefinition of lazy associations' getter methods somehow evaluate to true while acting as a proxy for a nil value (you'd know better than I would).
Anyway, I thought I'd just let you know - nil is now evaluating to true!
if you have
Fabricator(:post) do
comments(:count => 3)
end
and do Fabricate.build(:post)
or Fabricate.attributes_for(:post)
. The comment object is always created instead of being built or hashed as expected.
With Machinist I could do
Object.make(5)
And it would return an array with 5 objects. What would be the equivalent in Fabrication?
See gist: http://gist.github.com/578543
If I understand correctly, Fabricate should save the DB record (where as .build just returns an instance) so then the timestamp callbacks from AR or Mongo should populate those fields, right?
I know I could do after_create or whatnot, but this is unexpected behavior in my opinion.
I don't know if this is more because I'm new to Fabricator or because of a setting or what...
In my users_spec request spec, I have: @user = Fabricate.build(:user)
. My tests fail when, from everything I know, they should be passing (I say that because it works using FactoryGirl).
This is my user_fabricator.rb:
Fabricator :user do first_name Faker::Name.first_name last_name Faker::Name.last_name email { sequence(:email) {|n| "user#{n}@example.com" } } password "foobar" password_confirmation "foobar" # A user has_many events events(count: (3)) { |user, i| Fabricate(:event, user: user) } end
In the rails console, I run @user = Fabricate.build(:user)
and generate a User. I run it again and it generates a User with the same :first_name
and :last_name
. (The :email
changes because it is a sequence). I would prefer to use Faker for the email address as well, but can't figure out how to do it. Is there a way I can force it to use different attributes?
I'm using factory_girl right now but the enhanced Mongoid, Cucumber, and generator support is attracting me to fabricator. However, I like to keep the factories in a root path instead of nested under test or spec since they are also shared with the features. Factory Girl maintains an array setting of load paths that can be easily customized and I'd like similar behavior in Fabrication.
I have this code:
require 'fabrication'
class Stuff
attr_accessor :thing
end
Fabricator(:stuff) do
puts 'called!' # printed once
thing sequence(:sequence)
end
puts Fabricate.build(:stuff).thing # => 0
puts Fabricate.build(:stuff).thing # => 0
Which I think ought to print 0 and 1. I guess Fabrication is caching the generated objects, but unless I'm totally misunderstanding something, that sorta defeats the purpose of sequences. Am I misunderstanding something basic here?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.