Unit Test newbie problem

Hi All,

I got a set of tests for a Ruby app. The first fails unexpectedly
(“wrong number of args”). I added code to show that the app works as
I think it should. Any ideas?

The code is below:

I’m running:
Rails 2.3.5, Ruby 1.8.6, WinXP-Pro/SP3, Firefox 3.6.2, Firebug 1.5.3,
MySQL 5.0.37-community-nt, Mongrel, Apache HTTP Server 2.2.15

Thanks in advance,
Richard

GenModCRUD.rb <<<<<<<<<<<<< The Program >>>>>>>>>>>>>>>

module GMC_mod

The standard views (in app.views) in alphabetical order

for any DB table

:private
def crud_views(type)
case type
when :string
s = %w[edit index new show].join(", ")
when :symbols
s = [:editm, :index, :new ,:show]
puts s
else
raise ‘Invalid arg for crud_views: “%s”’ % type
end
s
end

:public
def crud_views_as_a_string
crud_views(:string)
end

def crud_views_as_a_symbols_array
crud_views(:symbols)
end
end

class InlineTest
include GMC_mod
end

t = InlineTest.new
puts t.crud_views_as_a_string.inspect # => “edit, index, new, show”

GenModCRUD.rb <<<<<<<<<<<<< The Test >>>>>>>>>>>>>>>

require ‘test/unit’
require ‘GenModCRUD’

class MyTests < Test::Unit::TestCase
include GMC_mod

def test_001_crud_views_as_a_string
assert_equal( “edit, index, new, show”,
crud_views_as_a_string(:string), “Surprise”)
end

end

<<<<<<<<<<<<< The Results >>>>>>>>>>>>>>>
edit, index, new, show"
Loaded suite TestGMC
Started
E
Finished in 0.0 seconds.

  1. Error:
    test_001_crud_views_as_a_string(MyTests):
    ArgumentError: wrong number of arguments (1 for 0)
    TestGMC.rb:12:in crud_views_as_a_string' TestGMC.rb:12:intest_001_crud_views_as_a_string’

1 tests, 0 assertions, 0 failures, 1 errors

RichardOnRails wrote:

Hi All,

I got a set of tests for a Ruby app. The first fails unexpectedly
(“wrong number of args”). I added code to show that the app works as
I think it should. Any ideas

You’re getting that error on line 12. What’s on line 12? (It’s hard to
tell which blank and comment lines are really in your code.)

The code is below:

I’m running:
Rails 2.3.5, Ruby 1.8.6, WinXP-Pro/SP3, Firefox 3.6.2, Firebug 1.5.3,
MySQL 5.0.37-community-nt, Mongrel, Apache HTTP Server 2.2.15

There is no reason to give us the details of your browser when
discussing server-side code.

Thanks in advance,
Richard

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On May 17, 4:43 am, RichardOnRails
[email protected] wrote:

module GMC_mod

The standard views (in app.views) in alphabetical order

for any DB table

:private

Not relevant to the problem but this doesn’t actually make methods
private (it’s just the symbol literal ‘private’). Remove the : for it
to actually do something (similarly for the public later on)

Fred

Hi Marnen and Frederick,

Thanks for looking into my problem.

Frederick: I removed the colons from the public and private macros
Marnen: The code and the output I got from running them is at
http://www.pastie.org/963644. It’s got line numbers. As you
obviously can now see, line 12 is the second line in::

assert_equal( "edit, index, new, show",

crud_views_as_a_string(:string), “Surprise”)

Thanks again,
Richard

On May 17, 5:32 am, Frederick C. [email protected]

On May 17, 1:42 pm, RichardOnRails
[email protected] wrote:

Actually it says it’s line 12 in TestGMC.rb which isn’t one of the
files listed. What’s in there ?

Fred

OK guys,

All problems solved.
As you suspected, no doubt, all problems were my errors. But I lacked
faith in Unit Tests so I failed to diligently investigate how reported
errors may be caused by defects in my code.

But after posting the code for a second time, I started to notice
some strange code fragments. Ultimately I found three defects in my
app that caused the three unexpected things in the test output. As I
said, all defects have been ameliorated, resulting in a clean Unit
Test report.

Thank you both for continuing to help be gain strength, little by
little, in Ruby/Rails programming.

Best wishes,]
Richard

On May 17, 10:04 am, RichardOnRails

The problem is that the function crud_views_as_a_string is not defined
(GenModCRUD.rb:22:def crud_views_as_a_string') to take an argument. But in your test you are calling it with an argument :string (TestGMC.rb:12:crud_views_as_a_string(:string)’

I’m not sure what you’re testing, but to simply remove the error, you
would want TestGMC.rb:12 line to read:

assert_equal( “edit, index, new, show”, crud_views_as_a_string(),
“Surprise”)

On May 16, 8:43 pm, RichardOnRails

Minor correction:

View http://www.pastie.org/963750 instead of the “pastie” link I
provided earlier.

My minor error was that the first line of TestGMC.rb was a comment
with the wrong filename. That led me to post incorrect labels in the
“pastie”.

I apologize for the sloppiness.

Best wishes,
Richard

On May 17, 8:42 am, RichardOnRails

Thanks for your help. My faith the Unit Test really works is
strengthening :slight_smile:

RichardOnRails wrote:

Thanks for your help. My faith the Unit Test really works is
strengthening :slight_smile:

Excellent! When you get a chance, try RSpec, see how much nicer it is,
and watch yourself achieve enlightenment!

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Will do!

Best wishes,
Richard