SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Apr 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SR2 - testing stories association - getting failures

    I'm on page 286 of Simply Rails 2, and having another issue with testing (does anybody seem to have more problems with the writing the tests than they do the actual code?).

    Specifically the method is

    Code:
      def test_should_have_a_stories_association
        assert_equal 2, users(:patrick).stories.size
        assert_equal stories(:one), users(:patrick).stories.first
      end
    And I believe it is the 2nd assertion that is giving an error.
    There are 2 stories that are in the yml file, and the "first" record that is being returned is the 2nd one in the order in which they are in.

    Now, I came across a similar (I think) issue in the previous chapter, where the errata said that there are issues with the ordering of yml test data due to the way that they were inserted, and recommended forcing the order by giving them a forced id. This worked for that, but not for this.
    Attempting to do that just gave nil returns everywhere.

    The other option given in the errata was to use the created_at to find the most recent entry.
    Will that work in this situation? And how is that actually coded? I can't seem to find anything that will return me the entry. The only way I could get it to work just returned me the created_at time.

    Thanks in advance!

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe the problem is that the id isn't specified in the YAML fixture; it was assumed that the id would be in the order that the records appeared in the YAML file. However, that isn't always the case. This has been commented on a number of times in this forum.

    You have three options I think:
    1. Add ids to the YAML files
    2. Test on created_at
    3. Test for presence of

    Add ids to the YAML files
    Probably the simplest option, and personally the one I'd do. Just add a line to the fixture for each record. For example, for the first record:
    Code:
    one:
      id: 1
    Obviously you increment the digit for each record (so the next item has id: 2 and so on).

    That way you know what is going into the database, which makes it easier to track errors (for example when tracing problems with foreign keys). Personally, I always specify ids in my fixtures.

    Test on created_at
    Update the stories has_many relationship in the User model so that it returns storied in the order they were created:
    Code:
      has_many :stories, :order => 'created_at'
    Then user.stories.first will return the stories with the earliest create_at first

    [*]Test for presence of
    If you aren't really bothered about the order and just want to make sure that the stories contain a particular story, you can change the second line to:
    Code:
      assert users(:patrick).stories.include?(stories(:one))

  3. #3
    SitePoint Member
    Join Date
    Apr 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ReggieB View Post
    Add ids to the YAML files
    Probably the simplest option, and personally the one I'd do. Just add a line to the fixture for each record. For example, for the first record:
    Code:
    one:
      id: 1
    Obviously you increment the digit for each record (so the next item has id: 2 and so on).

    That way you know what is going into the database, which makes it easier to track errors (for example when tracing problems with foreign keys). Personally, I always specify ids in my fixtures.
    I'll keep this in mind for future stuff. However I did try to do this, but I just got a different set of errors. Mostly complaining about nil values. Maybe its got something to do with the order in which I'm arranging them... I'll take another look into it.


    Test on created_at
    Update the stories has_many relationship in the User model so that it returns storied in the order they were created:
    Code:
      has_many :stories, :order => 'created_at'
    Then user.stories.first will return the stories with the earliest create_at first
    Ah, I think this might have been what the author was referring to in the errata page of SR2.
    Is there no way to adjust the order on the fly (in the testing files, i mean), as opposed to changing it in the user model declarations?

    [*]Test for presence of
    If you aren't really bothered about the order and just want to make sure that the stories contain a particular story, you can change the second line to:
    Code:
      assert users(:patrick).stories.include?(stories(:one))
    This definitely seems to fit the idea of the test case better.

    Thanks heaps!


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
  •