# Unit testing problem

**URL:** <https://www.ruby-forum.com/t/unit-testing-problem/164723>\
**Category:** Rails\
**Created:** [April 4, 2009, 7:14pm UTC](https://www.ruby-forum.com/t/unit-testing-problem/164723 "2009-04-04T19:14:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [April 4, 2009, 7:14pm UTC](https://www.ruby-forum.com/t/unit-testing-problem/164723/1 "2009-04-04T19:14:43Z")

</div>

Hello list,

I am having following problem while running unit tests.

1. The test file contains:  
require File.dirname( **FILE** ) + ‘/…/test\_helper’  
class RegionTest \< Test::Unit::TestCase  
I receive following error when running individual test  
(./test/unit/region\_test.rb):  
./test/unit/region\_test.rb: line 1: syntax error near unexpected token  
`(' ./test/unit/region_test.rb: line 1:`require File.dirname( **FILE** ) +  
‘/…/test\_helper’’

2. If I change these lines to:  
require ‘test\_helper’  
class RegionTest \< ActiveSupport::TestCase

I get following errors when running individual test  
(./test/unit/region\_test.rb):  
./test/unit/region\_test.rb: line 1: require: command not found  
./test/unit/region\_test.rb: line 2: ActiveSupport::TestCase: No such  
file or directory  
./test/unit/region\_test.rb: line 8: fixtures: command not found  
./test/unit/region\_test.rb: line 10: def: command not found  
./test/unit/region\_test.rb: line 11: region: command not found  
./test/unit/region\_test.rb: line 12: assert: command not found  
./test/unit/region\_test.rb: line 13: syntax error near unexpected token  
`(' ./test/unit/region_test.rb: line 13:` assert  
region.errors.invalid?(:name)’

The rake test runs successfully in both cases.

Also, rake test:test:rcov or rake:test:units:rcov is failing with error  
as,  
Command failed with status (1): [/usr/bin/ruby  
"/home/carlos/mytopics/…]

Rails version- 2.1.0 and 2.1.1 both are giving same problem.

Any clues?

Thanks,  
CS.

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [April 4, 2009, 7:37pm UTC](https://www.ruby-forum.com/t/unit-testing-problem/164723/2 "2009-04-04T19:37:54Z")

</div>

Carlos S. wrote:

Luv your jams, man!

> ‘/…/test\_helper’’  
> Prepare to smack your forehead. I get that running this:

$ sh test/unit/merchant\_test.rb  
test/unit/merchant\_test.rb: 1: Syntax error: “(” unexpected

That’s sh, meaning bash, not Ruby. Whatever runs your tests needs an  
explicit  
‘ruby’ in it. That is the way of things.

> Also, rake test:test:rcov or rake:test:units:rcov is failing with error  
> as,  
> Command failed with status (1): [/usr/bin/ruby  
> "/home/carlos/mytopics/…]

Dunno - do you have rcov, whatever that is, installed?

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [April 4, 2009, 8:03pm UTC](https://www.ruby-forum.com/t/unit-testing-problem/164723/3 "2009-04-04T20:03:15Z")

</div>

Thanks Phlip…

Its working if I explicitly specify ruby now.

However doing rake test:units still fails and so does  
rake:test:test:rcov or rake test:units:rcov.

The error with rake test:units is:  
rake aborted!  
Command failed with status (1): [/usr/bin/ruby -Ilib:test  
“/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake/rake\_test\_loader.rb”  
“test/unit/role\_test.rb” “test/unit/attachment\_test.rb”  
“test/unit/section\_test.rb” “test/unit/region\_test.rb” "test/uni…]  
/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:974:in `sh' /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:987:in`call’  
/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:987:in `sh' /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:1084:in`sh’  
/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:1019:in `ruby' /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:1084:in`ruby’  
/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake/testtask.rb:117:in  
`define' /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:1102:in`verbose’  
/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake/testtask.rb:102:in  
`define' /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:621:in`call’  
/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:621:in `execute' /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:616:in`each’  
/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:616:in `execute' /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:582:in`invoke\_with\_call\_chain’  
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize' /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:575:in`invoke\_with\_call\_chain’  
/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:568:in `invoke' /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:2031:in`invoke\_task’  
/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:2009:in `top_level' /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:2009:in`each’  
/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:2009:in `top_level' /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:2048:in`standard\_exception\_handling’  
/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:2003:in `top_level' /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:1982:in`run’  
/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:2048:in  
`standard_exception_handling' /usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake.rb:1979:in`run’  
/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/bin/rake:31  
/usr/bin/rake:19:in `load’  
/usr/bin/rake:19

However, following is successful:  
/usr/bin/ruby -Ilib:test  
“/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake/rake\_test\_loader.rb”  
“test/unit/region\_test.rb”

Any clues?

Phlip wrote:

> Carlos S. wrote:
> 
> Luv your jams, man!
> 
> > ‘/…/test\_helper’’  
> > Prepare to smack your forehead. I get that running this:
> 
> $ sh test/unit/merchant\_test.rb  
> test/unit/merchant\_test.rb: 1: Syntax error: “(” unexpected
> 
> That’s sh, meaning bash, not Ruby. Whatever runs your tests needs an  
> explicit  
> ‘ruby’ in it. That is the way of things.
> 
> > Also, rake test:test:rcov or rake:test:units:rcov is failing with error  
> > as,  
> > Command failed with status (1): [/usr/bin/ruby  
> > "/home/carlos/mytopics/…]
> 
> Dunno - do you have rcov, whatever that is, installed?

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [April 4, 2009, 9:55pm UTC](https://www.ruby-forum.com/t/unit-testing-problem/164723/4 "2009-04-04T21:55:36Z")

</div>

In theory, you should be able to run just one test suite with

ruby test/unit/role\_test.rb

However, the Rails 2 generator f—ed that up; no idea why. The  
maintainers were  
formerly very good at keeping the tests flexible.

But…

Carlos S. wrote:

> However doing rake test:units still fails and so does  
> rake:test:test:rcov or rake test:units:rcov.

I think those rcov things are a test coverage issue. If you use TDD you  
should  
not need them.

> The error with rake test:units is:  
> rake aborted!  
> Command failed with status (1): [/usr/bin/ruby -Ilib:test  
> “/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake/rake\_test\_loader.rb”  
> “test/unit/role\_test.rb” “test/unit/attachment\_test.rb”  
> “test/unit/section\_test.rb” “test/unit/region\_test.rb” "test/uni…]

Did you try rake test:units --trace ? or is this its output?

Try this rake\_test\_loader.rb thing on each test suite.

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [April 5, 2009, 1:32am UTC](https://www.ruby-forum.com/t/unit-testing-problem/164723/5 "2009-04-05T01:32:07Z")

</div>

Phlip wrote:

> In theory, you should be able to run just one test suite with
> 
> ruby test/unit/role\_test.rb
> 
> However, the Rails 2 generator f—ed that up; no idea why. The  
> maintainers were  
> formerly very good at keeping the tests flexible.
> 
> But…
> 
> Carlos S. wrote:
> 
> > However doing rake test:units still fails and so does  
> > rake:test:test:rcov or rake test:units:rcov.
> 
> I think those rcov things are a test coverage issue. If you use TDD you  
> should  
> not need them.
> 
> > The error with rake test:units is:  
> > rake aborted!  
> > Command failed with status (1): [/usr/bin/ruby -Ilib:test  
> > “/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake/rake\_test\_loader.rb”  
> > “test/unit/role\_test.rb” “test/unit/attachment\_test.rb”  
> > “test/unit/section\_test.rb” “test/unit/region\_test.rb” "test/uni…]
> 
> Did you try rake test:units --trace ? or is this its output?

Yeah, this is the output of rake test:units --trace

> Try this rake\_test\_loader.rb thing on each test suite.

Its working if I specify ruby and also with rake\_test\_loader.rb  
(/usr/bin/ruby -Ilib:test  
“/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake/rake\_test\_loader.rb”  
test/unit/user\_test.rb) specified.

The rake test is also working. Damn… I really need code coverage  
stats…  
Any help please? I will send u free Jam CD… 🙂

- 

CS.

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [April 5, 2009, 2:27am UTC](https://www.ruby-forum.com/t/unit-testing-problem/164723/6 "2009-04-05T02:27:04Z")

</div>

The test with rcov is working, however after completion tests its giving  
an error as,  
rake aborted!  
Command failed with status (1): [/usr/bin/ruby  
“/home/carlos/mytopics//vendor/plugins/rails\_rcov/tasks/rails\_rcov.rake”  
–run-rake-task=test…].

Well, previously the error message was truncated so I couldn’t see only  
part of it. I modified rake.rb file in sh method so as to get more info  
out of error message:

def sh(\*cmd, &block)  
options = (Hash === cmd.last) ? cmd.pop : {}  
unless block\_given?  
show\_command = cmd.join(" ")  
show\_command = show\_command[0,242] + “…”  
# TODO code application logic heref show\_command.length \> 45  
block = lambda { |ok, status|  
ok or fail “Command failed with status (#{status.exitstatus}):  
[#{show\_command}]”  
}  
…  
…  
end

* * *

However, there is significant difference between code coverage shown by  
rake test:test:rcov and rake test:units:rcov. The later one seems to be  
more accurate though.

Any clues?

- 

CS.

Carlos S. wrote:

> Phlip wrote:
> 
> > In theory, you should be able to run just one test suite with
> > 
> > ruby test/unit/role\_test.rb
> > 
> > However, the Rails 2 generator f—ed that up; no idea why. The  
> > maintainers were  
> > formerly very good at keeping the tests flexible.
> > 
> > But…
> > 
> > Carlos S. wrote:
> > 
> > > However doing rake test:units still fails and so does  
> > > rake:test:test:rcov or rake test:units:rcov.
> > 
> > I think those rcov things are a test coverage issue. If you use TDD you  
> > should  
> > not need them.
> > 
> > > The error with rake test:units is:  
> > > rake aborted!  
> > > Command failed with status (1): [/usr/bin/ruby -Ilib:test  
> > > “/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake/rake\_test\_loader.rb”  
> > > “test/unit/role\_test.rb” “test/unit/attachment\_test.rb”  
> > > “test/unit/section\_test.rb” “test/unit/region\_test.rb” "test/uni…]
> > 
> > Did you try rake test:units --trace ? or is this its output?
> 
> Yeah, this is the output of rake test:units --trace
> 
> > Try this rake\_test\_loader.rb thing on each test suite.
> 
> Its working if I specify ruby and also with rake\_test\_loader.rb  
> (/usr/bin/ruby -Ilib:test  
> “/usr/lib/ruby/gems/1.8/gems/rake-0.8.2/lib/rake/rake\_test\_loader.rb”  
> test/unit/user\_test.rb) specified.
> 
> The rake test is also working. Damn… I really need code coverage  
> stats…  
> Any help please? I will send u free Jam CD… 🙂
> 
> - 
> 
> CS.

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [April 5, 2009, 4:25am UTC](https://www.ruby-forum.com/t/unit-testing-problem/164723/7 "2009-04-05T04:25:51Z")

</div>

> The rake test is also working.

Disregard the question in the other post! Sheepish grin -\> {-;

Why do you need coverage stats? Is a general estimate enough? And does  
El Goog  
turn up any alternate coverage tools?

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [April 5, 2009, 4:20am UTC](https://www.ruby-forum.com/t/unit-testing-problem/164723/8 "2009-04-05T04:20:00Z")

</div>

Carlos S. wrote:

> However, there is significant difference between code coverage shown by  
> rake test:test:rcov and rake test:units:rcov. The later one seems to be  
> more accurate though.

I can’t tell from your posts if all the tests run!

Coverage is an advanced topic, and it’s utterly irrelevant if you have a  
broken  
build. Do you still have any symptoms to report?

I have never used rcov (and my test to code ratio is usually 2:1), so I  
feel  
compelled to recommend you get on rcov’s mailing list for specific  
questions  
about it.

A thinking-out-of-the-box suggestion; can you manually identify lines of  
code,  
or branches, that are not tested? That research would help you out with  
rcov’s  
mission statement - better coverage!

–  
Phlip
