SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member skrolikowski's Avatar
    Join Date
    Nov 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    unit & functional test newbie questions...

    Hi all,

    I wonder if anyone else is/was confused reading the Chapter 6 Unit and Functional test section...?

    A few questions I had are below:

    1) Within the file 'test/unit/story_test.rb' I don't exactly understand how adding the line fixtures :stories "makes sure that dummy data for our test is loaded into the database..." I'm guessing it has to do with naming of the file stories.yml in the 'fixtures' folder, but it seems to me that Ruby has a HUGE flaw in that the functionality of the code depends on files with associated names (in this case 'stories'). What if I were to accidently rename a file? Wouldn't the whole program crash?

    2) On the "unit test" test_should_require_name, why are we testing if an error is on the name attribute when we create a Story object with the name attribute always being blank? Won't this always pass as true? What's the purpose of adding this test?

    3) The second paragraph on page 177 states "...assert_select checks for the existence of one form element in which five p elements are nested..." Why does it say five when there are actually only 3 paragraph tags in the new form and the assertion function states :count => 3 ?

    4) In the "test_should_add_story" of the functional test, we check to see if the record has been saved to the database using new_record?. Why do we want "...the assertion to tell us if it hasn't been saved at this point..."? Should this statement be true without adding the exclamation mark (!), since we successfully added a new story record?

    Everything else seemed pretty straight forward even though I know I'll end up reading this chapter again. Thanks for any help anyone can give!!!

    - Shane

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) Yes it depends on the filename. Yes it will "crash" if you rename the file. This is not a flaw. You don't fully understand RoR's philosophy if you think it is ;-)

    2) Can you post some code? Tests always pass if you don't have bugs.

    3) I think you're right: it should say :count => 5. (or are there 5 nested p's: <p><p>...</p></p>?)

    4) Could you post the code?

  3. #3
    SitePoint Member skrolikowski's Avatar
    Join Date
    Nov 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    2) Here's the code of what I was talking about:
    Code:
    def test_should_require_name
      s = Story.create(:name => nil)
      assert s.errors.on(:name)
    end
    So are you testing the functionality of the assertion or the code you write?

    4)Here's the code of what I was talking about:
    Code:
    def test_should_add_story
      post :new, :story => {
        :name => 'test story',
        :link => 'http://www.test.com'
      }
      assert ! assigns(:story).new_record?
      assert_redirected_to :action => 'index'
      assert_not_nil flash[:notice]
    end
    Won't the first assert (in bold) come out true? Why is the (!) needed?

    Thanks!

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    2) You are testing that the validation is there. Yes, it's a silly test.

    4) Yes is will come out true. That's why there is a !. ! means not, and you can use not instead:

    Code:
    assert not assigns(:story).new_record?

  5. #5
    SitePoint Member skrolikowski's Avatar
    Join Date
    Nov 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But wouldn't this statement:
    Code:
    assert assigns(:story).new_record?
    (without the exclamation point) come out true?

  6. #6
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No. new_record? returns true if the object hasn't been saved yet and false otherwise.

    Code:
    s = Story.new(...)
    s.new_record? # => true
    s.save
    s.new_record? # => false

  7. #7
    SitePoint Guru mattymcg's Avatar
    Join Date
    Oct 2005
    Location
    Melbourne, Australia
    Posts
    574
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by skrolikowski View Post
    Why does it say five when there are actually only 3 paragraph tags in the new form and the assertion function states :count => 3 ?
    Fenrir2 is right, this is a typo and has been listed on the book's errata page.
    I design beautiful, usable interfaces. Oh, and I wrote a kids' book.
    Follow me on Twitter.
    Read my blog.
    Buy my book, Charlie Weatherburn and the Flying Machine.

  8. #8
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    No. new_record? returns true if the object hasn't been saved yet and false otherwise.

    Code:
    s = Story.new(...)
    s.new_record? # => true
    s.save
    s.new_record? # => false
    i think it is the opposite or maybe there is a mistake in the book. There tha author wants to make sure that at that point the object is in fact not saved yet so it uses the new_record? method to confirm that it is so by reversing its output (that should be false if the object isn't yet saved in the database) with a "!" so the return value to assert is true.

  9. #9
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    2) You are testing that the validation is there. Yes, it's a silly test.

    4) Yes is will come out true. That's why there is a !. ! means not, and you can use not instead:

    Code:
    assert not assigns(:story).new_record?
    2) it is not silly in my opinion. It is a test. There we are testing if the validation for empty fields works by setting explicity the object with an empty field to check if an error arise from the validation system.

  10. #10
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    !s.new_record? will return true if the record has been saved, so it's saved if the test passes. Is this wrong?

    2) If you do TDD it might not be a silly test. Then your tests are like a todo list. If you use tests to verify that tricky code doesn't contains bugs then it is silly because this is trivial code. Also, if you forget to put the validation in your model you will probably forget to test for it ;-).

  11. #11
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    !s.new_record? will return true if the record has been saved, so it's saved if the test passes. Is this wrong?

    2) If you do TDD it might not be a silly test. Then your tests are like a todo list. If you use tests to verify that tricky code doesn't contains bugs then it is silly because this is trivial code. Also, if you forget to put the validation in your model you will probably forget to test for it ;-).
    that is right i was confusing myself

    that method return false if the record/object is saved in the database (i.e. it isn't a new record) so here we are testing that is "true" that the record/object is not a new record by reversing the return value from false to true with a "!" in front of the method call.

  12. #12
    SitePoint Member skrolikowski's Avatar
    Join Date
    Nov 2006
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    No. new_record? returns true if the object hasn't been saved yet and false otherwise.

    Code:
    s = Story.new(...)
    s.new_record? # => true
    s.save
    s.new_record? # => false
    First of all, thanks to all for giving my initial questions attention.
    Okay, so now I'm getting confused again.
    Here's the original code from the book...
    Code:
    def test_should_add_story
      post :new, :story => {
        :name => 'test story',
        :link => 'http://www.test.com'
      }
      assert ! assigns(:story).new_record?
      assert_redirected_to :action => 'index'
      assert_not_nil flash[:notice]
    end
    Since the story was never saved shouldn't the assertion...
    Code:
    assigns(:story).new_record?
    equal true, thus adding an exclamation (!) make it equal false?
    Or by using "post" do we save the record?

  13. #13
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by skrolikowski View Post
    First of all, thanks to all for giving my initial questions attention.
    Okay, so now I'm getting confused again.
    Here's the original code from the book...
    Code:
    def test_should_add_story
      post :new, :story => {
        :name => 'test story',
        :link => 'http://www.test.com'
      }
      assert ! assigns(:story).new_record?
      assert_redirected_to :action => 'index'
      assert_not_nil flash[:notice]
    end
    Since the story was never saved shouldn't the assertion...
    Code:
    assigns(:story).new_record?
    equal true, thus adding an exclamation (!) make it equal false?
    Or by using "post" do we save the record?
    After "post" the validation occurs, but beacause we have provided all the required fields the validation passes and the object is already saved when we use that line of code to check if the object is a new record (i.e. is not yet saved in the database). We are only checking if it is true that it has been saved for sure.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •