Test::Unit::TestCase not resolving correctly

D

dkmd_nielsen

I have test case that is failing.

===============================================================
1) Failure: testProj(TestProject) [---------\TestProject.rb:26]:
<"068906"> expected but was <"68906">.
1 tests, 1 assertions, 1 failures, 0 errors
===============================================================

In the assignment of the class variable value, the first assert_equal
test case appears to be comparing the expected value to the input of
the class variable assignment...not the value returned by the
assigment. The two asserts that follow the first yield the correct
result, so I know the assignment of the class variable is correct. Any
suggestions as to what I might be doing wrong in the first assert?

===============================================================
Given the following in my test case:
===============================================================

class TestProject < Test::Unit::TestCase
def setup
@prj=Project.new # new project
end

def testProj
assert_equal('068906',@prj.proj='68906')
assert_equal(true,@prj.proj=='068906')
assert_equal(false,@prj.proj=='68906')
end
end

===============================================================
And given the following function being called to assign the value of
the class variable.
===============================================================

# Set the project number. # Project number gets tranlated to the
# format "nnnnnn", and is zero filled. Note: while zero filling is
# the proper display of the project number, it does cause problems
# when trying to find folder names.
def proj=(num)
if num.class == String
raise ArgumentError,"Project number #{num} must be in the form
<#>nnnnnn" if num !~/#*(\d{1,6})/
n = $1.to_i
elsif num.class == Fixnum
n = num
elsif num.class == Float
n = num.to_i
else
raise ArgumentError,"Project number #{num} is in an
unrecognizable format"
end
@proj = sprintf("%06d",n)
end

===============================================================
The following test case does work
1 tests, 2 assertions, 0 failures, 0 errors
===============================================================
def testProj
@prj.proj='68906'
# assert_equal("068906",@prj.proj='68906')
assert_equal(true,@prj.proj=='068906')
assert_equal(false,@prj.proj=='68906')
end


Your time, as always, is appreciated...
dvn
 
D

dblack

Hi --

In the assignment of the class variable value, the first assert_equal

s/class variable/instance variable/
test case appears to be comparing the expected value to the input of
the class variable assignment...not the value returned by the
assigment.

An assignment returns its right-hand side. This rule applies even to
pseudo-assignment method calls:

class C
attr_reader :x
def x=(y)
@x = 100
end
end

c = C.new
p c.x = 1 # prints 1, not 100
p c.x # prints 100

The idea is to make the assignment-like method calls truly
assignment-like.


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
L

Logan Capaldo

I have test case that is failing.

===============================================================
1) Failure: testProj(TestProject) [---------\TestProject.rb:26]:
<"068906"> expected but was <"68906">.
1 tests, 1 assertions, 1 failures, 0 errors
===============================================================

In the assignment of the class variable value, the first assert_equal
test case appears to be comparing the expected value to the input of
the class variable assignment...not the value returned by the
assigment. The two asserts that follow the first yield the correct
result, so I know the assignment of the class variable is correct.
Any
suggestions as to what I might be doing wrong in the first assert?

===============================================================
Given the following in my test case:
===============================================================

class TestProject < Test::Unit::TestCase
def setup
@prj=Project.new # new project
end

def testProj
assert_equal('068906',@prj.proj='68906')
assert_equal(true,@prj.proj=='068906')
assert_equal(false,@prj.proj=='68906')
end
end

===============================================================
And given the following function being called to assign the value of
the class variable.
===============================================================

# Set the project number. # Project number gets tranlated to the
# format "nnnnnn", and is zero filled. Note: while zero filling is
# the proper display of the project number, it does cause problems
# when trying to find folder names.
def proj=(num)
if num.class == String
raise ArgumentError,"Project number #{num} must be in the
form
<#>nnnnnn" if num !~/#*(\d{1,6})/
n = $1.to_i
elsif num.class == Fixnum
n = num
elsif num.class == Float
n = num.to_i
else
raise ArgumentError,"Project number #{num} is in an
unrecognizable format"
end
@proj = sprintf("%06d",n)
end

===============================================================
The following test case does work
1 tests, 2 assertions, 0 failures, 0 errors
===============================================================
def testProj
@prj.proj='68906'
# assert_equal("068906",@prj.proj='68906')
assert_equal(true,@prj.proj=='068906')
assert_equal(false,@prj.proj=='68906')
end


Your time, as always, is appreciated...
dvn
obj.meth = x always returns x, regardless of what the method would
return in normal circumstances. You should test like this:

@prj.proj = '68906'

assert_equal( '068906', @prj.proj )


% cat assigns.rb
class A
def f=(x)
false
end
end

a = A.new
p a
p( a.f = 22 )

% ruby assigns.rb
#<A:0x1e9328>
22
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top