Every repository with this icon (
Every repository with this icon (
| Description: | Fixture replacement for focused and readable tests. edit |
-
0 comments Created 5 months ago by qrushShorthand for referencing sequences in Factory definitionsfeaturexReported by Eric Mill
#13 describes syntactic sugar for one-off sequences inside factory definitions. This request is to expand that, and allow for easy referencing of separately defined sequences.
Referencing a separately defined sequence by name:
Factory.define :user do |u| u.sequence :email, :user_email end Factory.sequence(:user_email {|n| "user#{n}@mail.com"}Auto-referencing a separately defined sequence by using the same name:
Factory.define :user do |u| u.sequence :email end Factory.sequence(:email) {|n| "user#{n}@mail.com"}And here's a crazier, but cooler idea at syntax, that assumes that a symbol passed as an attribute value refers to the name of a sequence:
Factory.define :user do |u| u.email :user_email end Factory.sequence(:user_email) {|n| "user#{n}@mail.com"}The last example would eliminate the need for supporting either of the first two.
Comments
-
2 comments Created 5 months ago by qrushUndefine or delegate common object methods on proxiesfeaturexReported by Joe Ferris
Proxy objects are responsible for returning values of previously defined attributes, but you can't reference "id" and other attributes that conflict with methods on Object.
Proxy should either undefine these methods so that method_missing is called, or directly delegate them to #get.
Comments
I've undefined type and id in: http://github.com/flori/factory_girl/tree/type-attribute
The type attribute is necessary in order to use rails' single table
inheritance feature. Unfortunately this collides with Ruby's deprecated
Kernel#type method and the Factory object's method missing isn't
triggered. This change undefines the type instance method in Factory and
fixes the problem.Factory.define :bar do |b| b.type 'Bar' end Factory.define :baz do |b| b.type 'Baz' end -
10 comments Created 5 months ago by qrushfeaturexafter_build, after_create hookspatchxReported by George Ogata
Hi,
I’ve found h-lame’s after_{build,create} callbacks a very useful addition to factory_girl. Only thing is his changes are on an old commit. I’ve merged his patch with your head in my fork [1], and was hoping you’d merge it in.
Comments
Comments from Paul Strong
+1 Complex data. Setting up state machines is a great example of why these callbacks would be useful.
This is a blocker for us. Instead of setting all the attributes by hand to simulate an activated user, I rather setup the factory to call an activate method on the user object which will create and modify all the associations necessary and take advantage of our state machine definitions.
Why are you guys so reluctant to merge this in?
These are the original modifications by h-lame:
http://github.com/oggy/factory_girl/commit/ce9214cfb9faa2846e3d3280b59e4a6571309b08
h-lame implemented the hooks as blocks and tobstarr implemented them as attributes of the define function. It would be nice to be able to do it both ways. Thx!
Comment from Ben Hughes
I strongly second being able to do this. Right now for more complex factories I'm actually creating separate helper methods that create child records, such as:
def post_with_comments Factory.create(:post).tap do |post| Factory.create(:comment, :post => post) Factory.create(:comment, :post => post) end endBeing able to define an after create filter and actually create a :post_with_comments factory that automatically created specified child records would be very useful for more complex factory situations.
I third this. Would be an absolutely fantastic feature to have and looks like it's mostly done.
+1 This would present a better solution for:
http://stackoverflow.com/questions/1054786/how-do-you-override-setinitialstate-from-aasm-when-testing-with-factory-girl-f+1, I have a model with a validation that there must be at least one child in a has_many relationship, so ideally I'd need the factory to call the after build callback before creating, then call the after create callback.
I implemented after_build and after_create callbacks recently for the latest factory girl. you can find it here: http://stackoverflow.com/questions/1506556/hasmany-while-respecting-build-strategy-in-factorygirl
I went even further making them inheritable and tested: http://github.com/nate/factory_girl/commits/callbacks
Who do I need to hitup about getting this pulled in? I didn't do documentation in case it didn't get accepted, but once it is I'd be willing to help out.
I've merged this in (nate's version), and bumped the gem to 1.2.3
-
Reported by Erik
The method variable_name_to_class_name has code that translates, say, 'active_record/base' to ActiveRecord::Base. But when I tried to use this, I got a 'wrong constant name' error, because constant names can't contain colons.
You can just supply a :class option, but it would be easier not to. I'm planning to fix the code and document the shortcut.
Here's my fix: http://github.com/eostrom/factory_girl/tree/guess_classes_in_modules
Comments
-
0 comments Created 5 months ago by qrushfeaturexAdd an ability to define default strategy globallypatchxReported by Peter Suschlik
Branch on github: http://github.com/splattael/factory_girl/tree/global_default_strategy
It's like
:default_strategyoption but for all factories:Factory.global_default_strategy = :build Factory.define(:post) {} ... Factory(:post) # => like Factory.build(:post)No need for
:default_strategy => :buildevery time.Note:
I don't like the name global_default_strategy.
Factory.default_strategy = :buildwould be nicer but I didn't want to hack up the current default_strategy method.Comments
-
6 comments Created 5 months ago by qrushfeaturexhas_many associations do not get persisted properlypatchxReported by Vladimir Andrijevik
Hi folks!
First off, I love factory_girl. Very well done, and thank you!
I'm writing to you about a weird issue I found with has_many associations in factories. I've tried to make this example as minimalistic as possible:
in test/factories.rb
Factory.define(:team) do |team| team.sequence(:name) { |n| "Team #{n}"} end Factory.define(:user) do |user| user.sequence(:name) { |n| "User #{n}"} user.association(:team) end Factory.define(:team_with_users, :parent => :team) do |team| team.users { |users| [users.association(:user)] } endWith these factories defined, trying to create a team with users in the tests looks like this:
team = Factory(:team_with_users) Team Create (0.5ms) INSERT INTO "teams" ("name", "updated_at", "created_at") VALUES('Team 1', '2009-05-11 17:12:47', '2009-05-11 17:12:47') User Create (0.4ms) INSERT INTO "users" ("name", "team_id", "updated_at", "created_at") VALUES('User 1', 996332878, '2009-05-11 17:12:47', '2009-05-11 17:12:47') Team Create (0.4ms) INSERT INTO "teams" ("name", "updated_at", "created_at") VALUES('Team 2', '2009-05-11 17:12:47', '2009-05-11 17:12:47') #As you can see, two teams are created, and the user object is associated with the wrong team. However, calling team.users will show the user object (because it's set with the users= setter), but on team.reload the users association appears empty.
team.users [#] team.reload.users []
Comments
masa-iwasaki
Tue Jun 23 07:40:07 -0700 2009
| link
I had a same issue and I built my fork to solve this problem. Try masa-iwasaki/factory_girl
masa-iwasaki
Tue Jun 23 07:40:45 -0700 2009
| link
I forgot to paste the url.
http://github.com/masa-iwasaki/factory_girl/tree/masterThis is sort of a big bug -- any chance it could be fixed?
advisorshq
Tue Aug 18 12:52:58 -0700 2009
| link
factory girl rocks... but having the same issue here!
Same issue. Seems like it should be a solved one. Either through fixing this or adding after_build and after_create blocks.
-
1 comment Created 5 months ago by qrushbugxfactories named with a module/class path can find the classpatchxReported by Alex Rothenberg
When I have a model class defined within a module FactoryGirl was unable to find the class correctly because it was not traversing down the module hierarchy.
The fix along with a spec that fails before and passes after are at http://github.com/alexrothenberg/factory_girl/commits/factories_for_classes_with_modules_patch
My scenario is that I have a model defined in a module
module BlogModels class Comment < ActiveRecord::Base end end
and want to define a factory for it like this
Factory.define 'blog_models/comment' do |comment| end
It fails with this error
1) NameError in 'Factory a factory defined with a module name should load the class defined inside the module' wrong constant name GroupingOfModels::Comment /Users/alexrothenberg/ruby/github/factory_girl/spec/../lib/factory_girl/factory.rb:304:in `const_get' /Users/alexrothenberg/ruby/github/factory_girl/spec/../lib/factory_girl/factory.rb:304:in `class_for' /Users/alexrothenberg/ruby/github/factory_girl/spec/../lib/factory_girl/factory.rb:58:in `build_class' ./spec/factory_girl/factory_spec.rb:306:
Patching the class_for method in factory.rb to go down the module hierarchy as it loads the class fixes the problem.
I hope this is not too much information in the ticket but I thought it was better to err with too much rather than too little :)
Thanks
AlexComments
-
When factory girl is loaded, this code does nothing when run in the rails console:
ActiveRecord::Base.logger = Logger.new(STDOUT)The expected behavior should be that ActiveRecord SQL statements are logged in the rails console. Factory_girl should not change this behavior.
Comments
-
1 comment Created 3 months ago by joshuaclaytonbugxFactories with parents defined don't have access to parent attributes when defining an attribute within a blockpatchxWhen creating a factory that has a parent defined, attributes defined in blocks don't have scope to parent attributes because the parent's attributes haven't been added to the factory yet.
I've created a branch that resolves this incorrect behavior: http://github.com/joshuaclayton/factory_girl/commits/parent-attributes-in-attribute-blocks
Comments
joshuaclayton
Mon Aug 10 11:48:06 -0700 2009
| link
Looks like it didn't pull in the Gist I created: http://gist.github.com/gists/165346
-
3 comments Created 3 months ago by roderickvdAdd support for singleton factoriesfeaturexSingleton factories are useful for creating unique instances, such as a fixed set of movie genres. The branch at http://github.com/roderickvd/factory_girl/tree/singletons adds the following syntax to do just that:
# Defines a new singleton Factory.define :comedy, :class => Genre, :singleton => true do |g| g.name 'Comedy' end
Because the singleton option is set on the factory, it works seamlessly for associations and all strategies.
Comments
So this creates a genre record named "Comedy" that is validated uniqueness of? Can't be deleted?
roderickvd
Mon Aug 24 01:26:29 -0700 2009
| link
You are right that it's validated uniqueness of. It could be deleted, but at any point in time there will be at most a single instance of "Comedy" as a genre record.
-
Reopening issue 12.
Ran into some trouble when trying to use factory sequences with spork + autospec—dozens of "Validation failed: Name has already been taken" errors. Changing Factory.find_definitions to use load instead of require takes care of the issue.
If anyone has a better suggestion, I'm open to it. If not, please consider merging this: http://github.com/jdhollis/factory_girl/commit/08fae46d01ad3faa7ecfc320e38f4696ac026041
The branch is here: http://github.com/jdhollis/factory_girl/tree/load-instead-of-require
Comments
-
Stubbing proxy fails on non-ActiveRecord classes
0 comments Created 2 months ago by sandroI've created a branch which makes the stubbing strategy more friendly for non-AR classes.
http://github.com/sandro/factory_girl/tree/stub_for_non_ar_objectsThe main issue is that the stubbing proxy calls #new which won't work if #initialize takes requires arguments. The fix is to fall back to #allocate when #new fails.
Comments
-
Hi,
There is a typo in README.rdoc. "startegy" instead of "strategy." Here is the commit you can pull in: akahn/factory_girl@8afb333
Cheers,
AlexComments
-
Factory.define :building, :class => Building::Mothership do |m|
m.x 0 m.x_end { |r| r.x + r.instance_variable_get("@instance").class.property('width') } endIt would be nice to just be able to
Factory.define :building, :class => Building::Mothership do |m|
m.x 0 m.x_end { |r, options| r.x + options[:class].property('width') } endComments
-
ActiveRecord and DataMapper share a significant amount of API. Enough at least to allow factory_girl to work with both. The following patch maintains compatibility.
Comments
I would also love to see DataMapper compatibility maintained. I've done some additional work to make the alternate syntaxes work with DM as well http://github.com/bgentry/factory_girl
The code works but there's an issue with DM not adding inclusions to Models if the models were loaded before the inclusion was added. I'm working to fix that in DataMapper right now.
Published a patch which should be included in the latest version of DataMapper, so my factory_girl changes above should be sufficient as soon as this commit is added to dm-core:
http://github.com/bgentry/dm-core/commit/2e64f793646040b55ce12bb43a3476cd4d661f04 -
Factory with parent fails inconsistently due to file load order
0 comments Created about 1 month ago by webgetProblem: a factory define that has a parent seems to be failing because the parent isn't loaded yet.
Example:
test/factories/person.rb Factory.define :person do |p| p.name 'Joe' end test/factories/person_with_extras.rb Factory.define :person_with_extras, :parent => :person do |p| p.age 21 endUsing the code above gives this error: No such factory: person.
Is it correct that a factory can have a parent like this, in a different file?
When I look in the gem factory.rb, it seems that the load order is inconsistent on line 276:
Dir[File.join(path, '*.rb')].each do |file|
I would expect at least for the files to be in alphabetic order:
- Dir[File.join(path, '*.rb')].each do |file| + Dir[File.join(path, '*.rb')].sort.each do |file|
Is there something obvious that I'm missing?
Or is it supposed to be this way?
Could the Thoughtbot team at least add the "sort" to the file list?
Much obliged,
Joel Parker Henderson
joelparkerhenderson@gmail.comComments
-
http://github.com/erikh/factory_girl
Patch adds proxy-specific Factory.define options.
Patch adds a :singleton_on attribute to Factory.define; see README.rdoc for use-case information, but the basic gist is that when going through the create proxy, the value of the :singleton_on parameter is used on the generated instance to find a target, which is then searched for in the model the instance was created from. If an object is found, it is used instead of creating a new one, and then saved.
Tests added, pull request sent, working in real-world scenarios already.
Comments
-
gemspec file fixed for 1.2.3 after integration of Nate patch
0 comments Created about 1 month ago by ookHi,
Just to point out the need to add lib/factory_girl/attribute/callback.rb file to the one included into gemspec, after Nate patch.
Commit's here: http://github.com/ook/factory_girl/commit/b7c6fb724e8f569d2452d1a1de75178af583ac69
Comments
-
README needs to describe new callbacks functionality
0 comments Created about 1 month ago by r00kI took a crack at it:
http://github.com/r00k/factory_girl/tree/update_readme_with_callbacksComments
-
README should point to gem on Gemcutter instead of GitHub
0 comments Created 28 days ago by chriskIt looks like you switched to Gemcutter for the 1.2.3 release, but the README still has installation instructions pointing to
thoughtbot-factory_girlon GitHub. I forked and fixed it:The commit is tagged
readme_gemcutterfor easy merging.Comments
-
We've got quite a few namespaced models in our codebase, and so far we've had no troubles specifying factories for them like so:
factory :namespace_foo, :class => Namespace::Foo do |nsf| nsf.foo 'foo' nsf.bar 'bar' endHowever now working with Spork in our specs, I'm trying to prevent the constants from being loaded so early, so I changed all the constants to strings.
factory :namespace_foo, :class => 'Namespace::Foo' do |nsf| nsf.foo 'foo' nsf.bar 'bar' endBut now all my factories break with
NameError Exception: wrong constant nameThis is just a simple matter of changing the way that factory_girl calls
Object#const_get. I'm forking and I'll link a branch with the fix shortly.Comments
Test case and fix pushed to this branch:
http://github.com/phinze/factory_girl/commits/issue28_factory_class_with_namespaceOne commit for the test case: http://github.com/phinze/factory_girl/commit/ddedf312568a9c66dfe6c1eb1be0c0bffc6fc83f
Another for the fix: http://github.com/phinze/factory_girl/commit/06b7b065e2477094f4e04e5f4601489578910ef1 -
Defining duplicate factories should raise an error
0 comments Created 22 days ago by r00kI've been bitten a few times recently by accidentally defining two factories with the same name. Factory_girl accepts this silently. I think an error is warranted: I can't think of a scenario where users are benefited by this ability and doing it inadvertently is likely to break things.
Here's my implementation. Attempting to define a second factory with the same name raises a DuplicateDefinitionError:
http://github.com/r00k/factory_girl/tree/disallow_duplicate_factory_definitionsComments











