Victor S
2011-May-15 03:23 UTC
Unit Testing getting me confused, not able to make test respond like application, where application is correct.
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: 2) Failure: test_should_have_loaded_this_fixture_item_into_the_test_database_already(CollectionTest) [test/unit/collection_test.rb:16]: Should not be valid 3) 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-May-15 10:36 UTC
Re: Unit Testing getting me confused, not able to make test respond like application, where application is correct.
On May 15, 4:23 am, Victor S <victor.s...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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> TEST REPORT: > > 2) Failure: > test_should_have_loaded_this_fixture_item_into_the_test_database_already(Co llectionTest) > [test/unit/collection_test.rb:16]: > Should not be valid > > 3) 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-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Victor S
2011-May-15 15:19 UTC
Re: Unit Testing getting me confused, not able to make test respond like application, where application is correct.
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" ? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Victor S
2011-May-15 15:23 UTC
Re: Unit Testing getting me confused, not able to make test respond like application, where application is correct.
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Victor S
2011-May-15 15:31 UTC
Re: Unit Testing getting me confused, not able to make test respond like application, where application is correct.
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.