Rake gives Command failed with status (1):

I am trying to run rake on an app I have made. This is what happens

neil@neil-laptop:~/************$ rake test:units --trace
(in /home/neil/************)
** Invoke test:units (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Execute db:test:load
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
** Execute test:units
/usr/bin/ruby1.8 -I"lib:test"
"/home/neil/.gem/ruby/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb"
"test/unit/story_test.rb" "test/unit/user_mailer_test.rb"
"test/unit/user_test.rb" "test/unit/helpers/community_helper_test.rb"
"test/unit/helpers/friendship_helper_test.rb"
"test/unit/helpers/profile_helper_test.rb" "test/unit/comment_test.rb"
"test/unit/friendship_test.rb"
ruby index.rb <data dir> <index dir>
rake aborted!
Command failed with status (1): [/usr/bin/ruby1.8 -I"lib:test"
"/home/neil/...]

Any ideas?

I'm still stuck, can't make an tests work

I have created a test application. It only has one model. When I run
tests on it it runs but still comes up with the same error message ie

  1) Failure:
test_should_not_be_valid_without_story(AnnnetteTest)
    [./test/unit/annnette_test.rb:6:in
`test_should_not_be_valid_without_story'
     /home/neil/annnette/vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:94:in
`__send__'
     /home/neil/annnette/vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:94:in
`run']:
<nil> is not true.

1 tests, 1 assertions, 1 failures, 0 errors
rake aborted!
Command failed with status (1): [/usr/bin/ruby1.8 -I"lib:test"
"/usr/lib/ru...]

Any ideas?

I have created a test application. It only has one model. When I run
tests on it it runs but still comes up with the same error message ie

1) Failure:
test_should_not_be_valid_without_story(AnnnetteTest)

So your test is failing. Either you have written the test incorrectly
or your application is not right. If you do not understand why the
test is failing show us the code of the test and we may be able to
help.

[./test/unit/annnette_test.rb:6:in

That is the line in your test where it is failing

`test_should_not_be_valid_without_story'
/home/neil/annnette/vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:94:in
`__send__'
/home/neil/annnette/vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:94:in
`run']:
<nil> is not true.

You have a test for something true, but it is actually nil.

Colin

This is the test, it looks OK to me.
require 'test_helper'

class AnnnetteTest < ActiveSupport::TestCase
def test_should_not_be_valid_without_story
a = Annnette.create(:story => 'Neil')
assert a.errors.on(:story)
end

end

Gleble

Please don't top post, it makes it difficult to follow the thread.
Insert your comments at appropriate point in previous message.
Thanks.

This is the test, it looks OK to me.
require 'test_helper'

class AnnnetteTest < ActiveSupport::TestCase
def test_should_not_be_valid_without_story
a = Annnette.create(:story => 'Neil')
assert a.errors.on(:story)

So what can you deduce from this? The test says a.errors.on(:story)
should be true. When you run the test it says it is nil. Your app is
allowing the create without generating errors. However the test looks
a bit odd. The name suggests that you are checking that you cannot
create an Annnette without a story, yet you appear to be giving it one
('Neil').

Colin

Colin Law wrote in post #962761:

Please don't top post, it makes it difficult to follow the thread.
Insert your comments at appropriate point in previous message.
Thanks.

This is the test, it looks OK to me.
require 'test_helper'

class AnnnetteTest < ActiveSupport::TestCase
def test_should_not_be_valid_without_story
a = Annnette.create(:story => 'Neil')
assert a.errors.on(:story)

So what can you deduce from this? The test says a.errors.on(:story)
should be true. When you run the test it says it is nil. Your app is
allowing the create without generating errors. However the test looks
a bit odd. The name suggests that you are checking that you cannot
create an Annnette without a story, yet you appear to be giving it one
('Neil').

Colin

So I change line 6 to

a = Annnette.create(:story => nil)

and I still get

1) Failure:
test_should_not_be_valid_without_story(AnnnetteTest)
    [./test/unit/annnette_test.rb:6:in
`test_should_not_be_valid_without_story'
     /home/neil/annnette/vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:94:in
`__send__'
     /home/neil/annnette/vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:94:in
`run']:
<nil> is not true.

1 tests, 1 assertions, 1 failures, 0 errors
rake aborted!
Command failed with status (1): [/usr/bin/ruby1.8 -I"lib:test"
"/usr/lib/ru...]

You will have to look at the code of the app and work out why.

Presumably somewhere in your code you have attempted to prevent the
creation without a story.

If you cannot see what is wrong with that code (the code that prevents
the creation without a story) then you could look at the Rails Guide
on debugging to help you to find out what is going on.

Colin

Colin Law wrote in post #962765:

class AnnnetteTest < ActiveSupport::TestCase

[./test/unit/annnette_test.rb:6:in
`test_should_not_be_valid_without_story'

/home/neil/annnette/vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:94:in

`__send__'

/home/neil/annnette/vendor/rails/activesupport/lib/active_support/testing/setup_and_teardown.rb:94:in

`run']:
<nil> is not true.

You will have to look at the code of the app and work out why.

Presumably somewhere in your code you have attempted to prevent the
creation without a story.

If you cannot see what is wrong with that code (the code that prevents
the creation without a story) then you could look at the Rails Guide
on debugging to help you to find out what is going on.

Colin

Presumably somewhere in your code you have attempted to prevent the
creation without a story.

That can't be so because this works

neil@baby6:~/annnette$ script/console
Loading development environment (Rails 2.2.3)

a = Annnette.create(:story => 'neil')

=> #<Annnette id: 3, story: "neil", created_at: "2010-11-20 12:58:11",
updated_at: "2010-11-20 12:58:11">

I am losing track of this conversation. I think we had better start
again. Can you explain:
1. What the test you wrote is attempting to check.
2. What code you have written in the application to perform the
operation that the test is checking.

Colin

You should possibly not be using .create as that does a save at that
point. The save will fail (because there's no story) and "a" won't be
assigned anything.

instead, try:
a = Annnette.new(:story => nil)

Or load a valid Annnette from your fixtures (or factories), set the
story to nil, and check the errors.

Colin Law wrote in post #962775:

=> #<Annnette id: 3, story: "neil", created_at: "2010-11-20 12:58:11",
updated_at: "2010-11-20 12:58:11">

I am losing track of this conversation. I think we had better start
again. Can you explain:
1. What the test you wrote is attempting to check.
2. What code you have written in the application to perform the
operation that the test is checking.

Colin

1.It should show an error if an attempt is made to create a story
without a story entry.
2.This is from annnettes_controller.rb

def create
    @annnette = Annnette.new(params[:annnette])

    respond_to do |format|
      if @annnette.save
        flash[:notice] = 'Annnette was successfully created.'
        format.html { redirect_to(@annnette) }
        format.xml { render :xml => @annnette, :status => :created,
:location => @annnette }
      else
        format.html { render :action => "new" }
        format.xml { render :xml => @annnette.errors, :status =>
:unprocessable_entity }
      end
    end
  end

It is a very simple app made with scaffold to try and understand the
problems with my main app which started this thread.

I don't see anything there stopping a save if there is no story.
Anyway this code is not called when you call Annnette.create, the
create method of the model is called. As Michael has pointed out you
probably want to use new rather than create in the test, but you still
need code to stop the save if there is no story. Usually this would
be done by a validation in the controller.

Colin

But you're testing a Model... so the controller code is not very
useful to help with what's going on :-/

Michael Pavling wrote in post #962781:

Michael Pavling wrote in post #962781:

But you're testing a Model... so the controller code is not very
useful to help with what's going on :-/

There is no code in models/annette.rb

no "validates_presence_of :annnette"?

I would be happy if any test worked

If you're writing code to validate db updates in your controller, and
trying to test them with unit tests, I suggest you take a couple of
steps back, buy and read the "Agile Web Development With Rails" book,
and move forward from there.

Regards,

If you just want a test that works then change your existing test to
one that checks that you _can_ create an object if you _do_ provide a
story, use new instead of create and change the assert to assert_nil,
something like

class AnnnetteTest < ActiveSupport::TestCase
def test_should_be_valid_with_story
a = Annnette.new(:story => 'Neil')
assert_nil a.errors.on(:story)
end

Colin

Colin Law wrote in post #962795:

class AnnnetteTest < ActiveSupport::TestCase
def test_should_be_valid_with_story
a = Annnette.new(:story => 'Neil')
assert_nil a.errors.on(:story)
end

Colin

OK that worked so I changed the test in my other app (the one that
matters) to

class StoryTest < ActiveSupport::TestCase
  def test_should_be_valid_with_author
    s = Story.create(:author => 'neil', :title => 'Story', :body =>
'test')
    assert s.errors.on(:author)
  end

When I run the test I still get

neil@baby6:~/********$ rake test:units
(in /home/neil/********)
/usr/bin/ruby1.8 -I"lib:test"
"/usr/lib/ruby/1.8/rake/rake_test_loader.rb" "test/unit/comment_test.rb"
"test/unit/user_mailer_test.rb" "test/unit/friendship_test.rb"
"test/unit/helpers/friendship_helper_test.rb"
"test/unit/helpers/profile_helper_test.rb"
"test/unit/helpers/community_helper_test.rb" "test/unit/user_test.rb"
"test/unit/story_test.rb"
ruby index.rb <data dir> <index dir>
rake aborted!
Command failed with status (1): [/usr/bin/ruby1.8 -I"lib:test"
"/usr/lib/ru...]

Gleble

Colin Law wrote in post #962795:

class AnnnetteTest < ActiveSupport::TestCase
def test_should_be_valid_with_story
a = Annnette.new(:story => 'Neil')
assert_nil a.errors.on(:story)
end

Colin

OK that worked so I changed the test in my other app (the one that
matters) to

class StoryTest < ActiveSupport::TestCase
def test_should_be_valid_with_author
s = Story.create(:author => 'neil', :title => 'Story', :body =>
'test')

What is that create doing there?

assert s.errors.on(:author)

That should be assert_nil if you expect no errors.

end

When I run the test I still get

neil@baby6:~/********$ rake test:units
(in /home/neil/********)
/usr/bin/ruby1.8 -I"lib:test"
"/usr/lib/ruby/1.8/rake/rake_test_loader.rb" "test/unit/comment_test.rb"
"test/unit/user_mailer_test.rb" "test/unit/friendship_test.rb"
"test/unit/helpers/friendship_helper_test.rb"
"test/unit/helpers/profile_helper_test.rb"
"test/unit/helpers/community_helper_test.rb" "test/unit/user_test.rb"
"test/unit/story_test.rb"
ruby index.rb <data dir> <index dir>
rake aborted!
Command failed with status (1): [/usr/bin/ruby1.8 -I"lib:test"
"/usr/lib/ru...]

That is a completely different error, it is rake that is aborting, not
the test that is failing (that is why it says 'rake aborted'). It has
not got as far as running the tests, so the content of the tests is
immaterial. Show us the rest of the message - but first look through
it yourself and see if you can see what is the problem, or at least
some hint as to the problem. The next bit that you have not showed us
may well be the key.

Colin

Colin Law wrote in post #962810:

That is a completely different error, it is rake that is aborting, not
the test that is failing (that is why it says 'rake aborted'). It has
not got as far as running the tests, so the content of the tests is
immaterial. Show us the rest of the message - but first look through
it yourself and see if you can see what is the problem, or at least
some hint as to the problem. The next bit that you have not showed us
may well be the key.

Colin

That's what I thought at first. I built the test app t try and make sure
the test bit worked . Anyhow heres the whole thing.

Gleble

neil@baby6:~/*****$ rake test:units --trace
(in /home/neil/******)
** Invoke test:units (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Execute db:test:load
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
** Execute test:units
/usr/bin/ruby1.8 -I"lib:test"
"/usr/lib/ruby/1.8/rake/rake_test_loader.rb" "test/unit/comment_test.rb"
"test/unit/user_mailer_test.rb" "test/unit/friendship_test.rb"
"test/unit/helpers/friendship_helper_test.rb"
"test/unit/helpers/profile_helper_test.rb"
"test/unit/helpers/community_helper_test.rb" "test/unit/user_test.rb"
"test/unit/story_test.rb"
ruby index.rb <data dir> <index dir>
rake aborted!
Command failed with status (1): [/usr/bin/ruby1.8 -I"lib:test"
"/usr/lib/ru...]
/usr/lib/ruby/1.8/rake.rb:995
/usr/lib/ruby/1.8/rake.rb:1010:in `call'
/usr/lib/ruby/1.8/rake.rb:1010:in `sh'
/usr/lib/ruby/1.8/rake.rb:1094:in `sh'
/usr/lib/ruby/1.8/rake.rb:1029:in `ruby'
/usr/lib/ruby/1.8/rake.rb:1094:in `ruby'
/usr/lib/ruby/1.8/rake/testtask.rb:117
/usr/lib/ruby/1.8/rake.rb:1112:in `verbose'
/usr/lib/ruby/1.8/rake/testtask.rb:102
/usr/lib/ruby/1.8/rake.rb:636:in `call'
/usr/lib/ruby/1.8/rake.rb:636:in `execute'
/usr/lib/ruby/1.8/rake.rb:631:in `each'
/usr/lib/ruby/1.8/rake.rb:631:in `execute'
/usr/lib/ruby/1.8/rake.rb:597:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/usr/lib/ruby/1.8/rake.rb:590:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/rake.rb:583:in `invoke'
/usr/lib/ruby/1.8/rake.rb:2051:in `invoke_task'
/usr/lib/ruby/1.8/rake.rb:2029:in `top_level'
/usr/lib/ruby/1.8/rake.rb:2029:in `each'
/usr/lib/ruby/1.8/rake.rb:2029:in `top_level'
/usr/lib/ruby/1.8/rake.rb:2068:in `standard_exception_handling'
/usr/lib/ruby/1.8/rake.rb:2023:in `top_level'
/usr/lib/ruby/1.8/rake.rb:2001:in `run'
/usr/lib/ruby/1.8/rake.rb:2068:in `standard_exception_handling'
/usr/lib/ruby/1.8/rake.rb:1998:in `run'
/usr/bin/rake:28