-
Website
http://www.dcmanges.com/blog -
Original page
http://www.dcmanges.com/blog/38 -
Subscribe
All Comments -
Community
-
Top Commenters
-
thelucid
3 comments · 1 points
-
jamiew
2 comments · 6 points
-
Daniel Ha
1 comment · 407 points
-
floehopper
1 comment · 1 points
-
leed25d
1 comment · 1 points
-
-
Popular Threads
Thanks
For my controller and view specs, I have an elaborate set of helper methods that construct fake model objects, with overridable default parameters (as in your fixture factory) and fake associations. (My mock_model method takes a hash of associations, stubs a couple of methods on the current mock object, and also stubs a reverse method on the associated mocks.)
Rspec advises hitting the database in model specs, so I can't use my mock helpers, but I got really used to the convenience of them. So I've started building a parallel set of model helpers that speed up the tedious "make the object valid" part. My favorite is create_spec_user, which generates a valid password, password confirmation (demanded by restful_authentication's User class), and unique email, based on the spec-provided login or a default.
I've seen some people take the philosophical stance that test objects (and mocks) should be static and not smart, to ensure you're always testing the same thing. But so far I've found this approach helps me focus on the things I'm actually trying to test, instead of the drudgery of fixture maintenance.
Just wondering... have you tried this on edge ?
The relationship building you do (ie: :paperbody => create_paperboy), doesn't seem to save the association.
Also, can't wait for you to post your modifications to this :)
i also got the file uploads working by creating a private function in the Factory module.
def file_upload(path, mime_type = nil)
ActionController::TestUploadedFile.new(
Test::Unit::TestCase.respond_to?(:fixture_path) ? \ Test::Unit::TestCase.fixture_path + path : path,
mime_type)
end
this is actually just the code from within the fixture_file_upload method but i couldn't get that to work from within the module. so it works just like fixture_file_upload
ie. file_upload('/docs/test1.ppt', 'application/vnd.ms-powerpoint')
i was wondering how one would go about including fixture_file_uploads in the creates.
http://replacefixtures.rubyforge.org/
There is also a screencast here: http://railsnewbie.com/files/fixture_replacemen... (the screencast features your site - hope you don't mind the free promotion :) I would be interested in your feedback on the plugin.
Thanks again.
Don't know if you gotten the chance to check out my plugin yet, but just wanted to let you know that it now works with attr_protected.
Hope all is well,
Scott
Obviously, this has some impact on the speed of the test (not sure if it's really noticeable or not), but the bigger problem for my project is that it messes up tests that check how many records are created or destroyed. For instance, I have test that checks to make sure the number of Users is one greater after performing a certain action. This method will break that.
Perhaps that's not a good way to test record creation and deletion, but it is pretty common - I believe the tests that are output when you use scaffold have a similar style. I wonder if there is a simple way to lazily evaluate the code in default_attributes? Perhaps wrapping each expression in a Proc or something?
create_customer(:paperboy => nil)
And this way no paperboy would be created. Or, you could just remove it from your defaults. Your call, ultimately.
Sounds cool. I'll check out FixtureReplacement. Thanks for creating it!
Ben
For any given model (let's say person), you'll have a test/unit/models/person_test.rb that uses unit_record and a test/functional/models/person_test.b that uses a factory and other database logic.
--- trunk/website/vendor/plugins/model_factory/lib/factory_builder.rb (revision 50)
+++ trunk/website/vendor/plugins/model_factory/lib/factory_builder.rb (revision 83)
@@ -46,14 +46,15 @@
Factory.send :define_method, :"valid_#{factory_name.to_s}_attributes" do
instance_variable_set("@default_attributes", @default_attributes)
+ result = default_attributes.dup
# if they stored a proc, make it nil
- default_attributes.each do |key, value|
- default_attributes[key] = value.call if value.is_a?(Proc)
+ result.each do |key, value|
+ result[key] = value.call if value.is_a?(Proc)
end
- default_attributes.select {|key, value| value == :rand }.each do |it|
+ result.select {|key, value| value == :rand }.each do |it|
types = factory.to_s.classify.constantize.const_get(it.first.to_s.pluralize.upcase)
- default_attributes[it.first] = types[rand(types.length)]
+ result[it.first] = types[rand(types.length)]
end
- return default_attributes
+ return result
end
:paperboy => attributes[:paperboy] || create_paperboy
You will end with two paperboys in the database otherwise.
Thanks!
Rick
http://spin.atomicobject.com/2008/04/30/model-g...
It's what I've been using for the past year or so.