Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > passing a method as a paramter

Reply
Thread Tools

passing a method as a paramter

 
 
Rick DeNatale
Guest
Posts: n/a
 
      03-02-2009
[Note: parts of this message were removed to make it a legal post.]

On Mon, Mar 2, 2009 at 1:41 PM, Sarah Allen <(E-Mail Removed)> 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, assword
> > 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 #<UserTest:0x1e93a58>
>


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, assword
end

private
def create(options={})
User.create({
:email => "(E-Mail Removed)",
:last_name => "jones",
assword => "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/pers...-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

 
Reply With Quote
 
 
 
 
Gary Wright
Guest
Posts: n/a
 
      03-02-2009

On Mar 2, 2009, at 3:16 PM, Rick DeNatale wrote:
> 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:


Wow. +10 to Rick for his google-ruby-rails-test-unit-mentoring-fu

Gary Wright




 
Reply With Quote
 
 
 
 
Sarah Allen
Guest
Posts: n/a
 
      03-02-2009
Rick Denatale wrote:
> Okay, I'm curious where you found the topfunky power tools plugin.
> There's not much that google reveals about the usage.


I'm just getting into TDD. I experimented with cucumber a bit and really
liked it, but I'm diving into an old codebase with lots of pre-existing
unit tests. I wanted to understand the basics, so (as you guessed) I
bought the peepcode tutorial.

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


If its wisdom is out of date, is there another resource you recommend?
Or is everyone just skipping unit tests these days in favor of the BDD
tools?

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


Yeah, I see that in retrospect -- seemed like a basic language question
at the time. Thanks so much for digging in and letting me know the
scoop, which I clearly didn't fully understand from the screencast. Of
course, now I know four ways to pass a method as a parameter, which is
fabulous.

Cheers,
Sarah
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing dereferenced new pointer to reference paramter shanemh@gmail.com C++ 7 10-30-2006 11:56 AM
paramter passing question diffuser78@gmail.com Python 2 08-09-2006 05:18 PM
SQL Output Paramter problem Islamegy® ASP .Net 1 04-16-2006 07:55 PM
Reuse paramter list and reuse connection tshad ASP .Net 5 05-17-2005 12:33 AM
Grabbing paramter in the URL Randy ASP .Net 3 02-08-2005 03:03 PM



Advertisments