Unit Testing getting me confused, not able to make test respond like application, where application

I have a few test cases, using unit test, I am new to testing but I
don’t
understand why, if fixtures are loaded and validations are in place (in
my
case, I am testing that identical objects should not be saved to the
database) – and I know that it works, because doing the test through
the UI
gives the right behavious, but the test does not behave as the actual
application does, and should…

TEST REPORT:

  1. Failure:
    test_should_have_loaded_this_fixture_item_into_the_test_database_already(CollectionTest)
    [test/unit/collection_test.rb:16]:
    Should not be valid

  2. Failure:
    test_should_not_save_with_duplicate_name_property(CollectionTest)
    [test/unit/collection_test.rb:11]:
    Saved a collection with a duplicate name

TEST CODE:

test “should not save with duplicate name property” do
c = collections(:one)
assert !c.save, ‘Saved a collection with a duplicate name’
end

test “should have loaded this fixture item into the test database
already”
do
c = collections(:one)
assert !c.valid?, ‘Should not be valid’
assert c.save, ‘Did not save first item’
c2 = collections(:one)
assert_equal c, c2, “Two items are not equal”
assert !c2.save, ‘Saved duplicate item as well’
end

On May 15, 4:23am, Victor S [email protected] wrote:

I have a few test cases, using unit test, I am new to testing but I don’t
understand why, if fixtures are loaded and validations are in place (in my
case, I am testing that identical objects should not be saved to the
database) – and I know that it works, because doing the test through the UI
gives the right behavious, but the test does not behave as the actual
application does, and should…

collections(:one) will get you an object that is already in the
database, calling save on it will just update it, not insert a
duplicate. Assuming that there is no other fixture with the same name
as collections(:one), there is no reason why it shouldn’t be valid.

Fred

Interesting, I thought I had to do a collections(:one).find for it to
be
taken out of the database, according to the documentation… but i did
notice, and my next question was going to be, why do I get an error when
doing the find method in the test?

  1. So, I guess it is not true that in order to get the values from
    the
    fixture you do this: “collections(:one)” ?
  2. And in order to get the values from the db you do this instead:
    “collections(:one).find” ?

For example this is the documentation at:

http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures

this will return the Hash for the fixture named david

users(:david)

using the find method, we grab the “real” david as a User

david = users(:david).find

OK i think I fixed it by doing this instead:

test “should not save with duplicate name property” do
c = Collection.new(collections(:one))
assert !c.save, ‘Saved a collection with a duplicate name’
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs