From: Rick DeNatale Date: 2009-03-03T05:16:42+09:00 Subject: Re: passing a method as a paramter --00163646d0d4a1dcdb046428831f Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Mon, Mar 2, 2009 at 1:41 PM, Sarah Allen wrote: > Rick Denatale wrote: > > Yes, you need to pass a symbol, just as the error is trying to tell you: > > > > def test_should_not_create_user_unless_default_fields > > assert_required_fields :create, :email, :lastname, :password > > end > > > > I'm assuming that this is in the unit test for the User model. > > So, how would it magically know to call create on User and not some > other class? as I might expect, I get this error: > > 1) Error: > test_should_not_create_user_unless_default_fields(UserTest): > NoMethodError: undefined method `create' for # > Okay, I'm curious where you found the topfunky power tools plugin. There's not much that google reveals about the usage. However, it appears to come from a very early Peepcode screen cast on Test First Design (Peepcode #4 to be exact). I did a quick look at the screencast and it depends on having helper method called ceate in the test case. So you should have something like: class UserTest < Test::Unit::TestCase #... def test_should_not_create_user_unless_default_fields assert_required_fields :create, :email, :lastname, :password end private def create(options={}) User.create({ :email => "fred@foo.com", :last_name => "jones", :password => "secret" }.merge(options) ) end end The create method creates a valid record, and the assert_required_fields nils out each field asserted to be required, calls the create method with that field set to nil and ensures that the resulting record has an error on that field. And given that helper method, the other test can be re-written as: def test_should_create_user user = create assert user.valid?, "User was invalid:\n#{user.to_yaml}" end Now, as I say, this plugin doesn't seem to be very popular based on a google search, and it's rather old. There are probably more 'modern' approaches to testing required field validations these days using Test::Unit tests in Rails, I'm not as up on that as I tend to use RSpec rather than Test::Unit. And since this really IS a Rails question, it probably would have gotten a better response, quicker if it had been submitted ot the ruby on rails mailing list. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale --00163646d0d4a1dcdb046428831f--