Giter VIP home page Giter VIP logo

fabrication's People

Contributors

adimichele avatar ajsharp avatar andrii avatar asiniy avatar bdmac avatar benedikt avatar brandonweiss avatar chanks avatar damsonn avatar davelyon avatar dx7 avatar elizabrock avatar erkattak avatar etehtsea avatar fny avatar gerhard avatar glebm avatar greis avatar hakanensari avatar ixti avatar joel avatar meredian avatar nashby avatar paulelliott avatar phoet avatar shayarnett avatar stas avatar superacidjax avatar tpope avatar yuki24 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fabrication's Issues

Sequel models aren't fabricating properly.

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!

Fabricator load order should not be important

# 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.

after_create Callbacks Executed After Build

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.

Set :class_name for namespaced objects

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

method name conflicts in Ruby 1.8.6

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.

Sequences not working?

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?

Running rspec <spec_file.rb> Fabricator doesnt seem to work

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.

Error in model generator

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

Mongoid Association Weirdness with Fabrication

(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?

How to fabricate for polymorphic association?

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?

Using callbacks - help needed

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.

Multiple embedded documents fabricator fail to fabricate object

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.

Feature Request: Fabricate.unique

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.

Uninitialized constant Fabrication (rails generate fabrication:cucumber_steps)

/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.

Attribute "break" breaks fabricator

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.

Fabricator reloading functionality is missing

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?

How do I generate a Hash?

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

Program cannot find fabricators

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.

  1. Fabrication::UnknownFabricatorError: No Fabricator defined for 'user'

and the slightly more puzzling

  1. Fabrication::UnfabricatableError: No class found for 'client'

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)

after_create callbacks don't stack

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.

German Umlauts not working

/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:inmap'
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:inrun'
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:inrun'
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:

  • abstract (1.0.0)
  • actionmailer (3.0.1)
  • actionpack (3.0.1)
  • activemodel (3.0.1)
  • activerecord (3.0.1)
  • activeresource (3.0.1)
  • activesupport (3.0.1)
  • arel (1.0.1)
  • autotest (4.4.1)
  • autotest-fsevent (0.2.3)
  • autotest-growl (0.2.6)
  • autotest-rails-pure (4.1.0)
  • bcrypt-ruby (2.1.2)
  • bson (1.1.1)
  • bson_ext (1.1.1)
  • builder (2.1.2)
  • bundler (1.0.3)
  • devise (1.1.3)
  • diff-lcs (1.1.2)
  • erubis (2.6.6)
  • fabrication (0.9.0)
  • i18n (0.4.1)
  • mail (2.2.7)
  • mime-types (1.16)
  • mongo (1.0.9)
  • mongoid (2.0.0.beta.19)
  • mongoid-rspec (1.2.1)
  • nokogiri (1.4.3.1)
  • polyglot (0.3.1)
  • rack (1.2.1)
  • rack-mount (0.6.13)
  • rack-test (0.5.6)
  • rails (3.0.1)
  • railties (3.0.1)
  • rake (0.8.7)
  • rspec (2.0.0)
  • rspec-core (2.0.0)
  • rspec-expectations (2.0.0)
  • rspec-mocks (2.0.0)
  • rspec-rails (2.0.1)
  • sys-uname (0.8.4)
  • thor (0.14.3)
  • timeliness (0.1.1)
  • treetop (1.4.8)
  • tzinfo (0.3.23)
  • validates_timeliness (3.0.0)
  • warden (0.10.7)
  • webrat (0.7.1)
  • will_paginate (3.0.pre2)

Fabricate.find_or_create_by feature

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 :)

lazy-loaded belongs_to associations only work once (was: Lazy-loaded associations don't work with :validates_presence_of)

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.

i want support generate the namespace(Inheritance).

[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

model relationships association with default fabricator

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.

Typo in docs for defining fabricators

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.

issue with mongoid model with 'type' field in REE

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

can't get attributes_for

Please see: http://pastie.org/1137371

Three things:

  1. I'm really liking fabricator. Thanks :)
  2. a = Fabricate.build :author actually works, but attributes_for does not. Why?
  3. I had expected the after_create method to also accept the count.

:count => 1 seems to cause an issue

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)] }

Bug/Question: how to make associations not create superflous objects ?

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?

Fabricator only generating object once?

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 ActiveRecord models have strange #clone behavior

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.

nil associations evaluate to true

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!

attribute_for and build ignored for relationships

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.

Create many objects

With Machinist I could do

Object.make(5)

And it would return an array with 5 objects. What would be the equivalent in Fabrication?

created_at and updated_at not populating

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.

Generating different data

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?

Feature Req: Custom schematic load paths

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.

Caching vs sequences

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.