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