SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Chapter 7 "Testing Ajax Voting" - confusion

    Hello everyone,

    I'm working my way through Patrick Lenz' very good book "Build your own Ruby on Rails web applications".
    I have applied all the errata from http://www.sitepoint.com/books/rails1/errata.php

    At the end of chapter 7 I ran into confusion and problems.
    Take a look at page 229 last code listing:

    def test_should_render_rjs_after_vote_with_ajax
    xml_http_request ost, :vote, :id => 2
    assert_response :success
    assert_template 'vote'
    end

    Then look at the next page, p.230: The second listing suggests that the function should really look like this:

    def test_should_render_rjs_after_vote_with_ajax
    xml_http_request ost, :vote, :id => 2
    post :vote, :id => storiesanother).id
    assert_response :success
    assert_template 'vote'
    end

    To make the confusion total, if you look at the source code from www.sitepoint.com/books/rails1/archive in the file 33-story_controller_test.rb
    then you will se a third version of the test function:

    def test_should_render_rjs_after_vote_with_ajax
    @request.env['HTTP_ACCEPT'] = 'text/javascript'
    post :vote, :id => 2
    assert_response :success
    assert_template 'vote'
    end


    So, which of the three is the correct one?

    Perhaps I should mention, that all three versions give me errors when I run the test.

    Regards
    Claus Holm, Copenhagen, Denmark
    Last edited by cleo; Apr 22, 2007 at 23:02.

  2. #2
    SitePoint Member
    Join Date
    Feb 2007
    Location
    Wiesbaden, Germany
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Charles,

    first of all, which is the exact error you're getting?

    Then, this would be the correct version of the test (the one shown on page 229), which passes fine for me:

    Code:
    def test_should_render_rjs_after_vote_with_ajax
      xml_http_request :post_with_user, :vote, :id => 2
      assert_response :success
      assert_template 'vote'
    end
    Additionally, since it invokes the `post_with_user helper`, here's a copy of that, just for completeness:

    Code:
      def post_with_user(action, parameters = nil, session = nil, flash = nil)
        post action, parameters, :user_id => users(:patrick).id
      end
    The test on page 230 definitely has an extra line that really shouldn't be there (the `post` call would actually issue a non-AJAX request).

    The third version that you found in the code archive would be just as good as the first version since what it does is manually setting the necessary headers to make the controller believe it's dealing with an AJAX request (which the `xml_http_request` helper takes care of for you).

    Best,
    Patrick

  3. #3
    SitePoint Addict nueva's Avatar
    Join Date
    Apr 2006
    Location
    Uk, London
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Some ppl had been complaining on this book because of the wrong code... As i heard on the sitepoint forum.

  4. #4
    SitePoint Addict nueva's Avatar
    Join Date
    Apr 2006
    Location
    Uk, London
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


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
  •