SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Test won't work

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Test won't work

    Can anbody explain why this test doesn't work?
    Code:
    require 'test_helper'
    
    class UserTest < ActiveSupport::TestCase
      
      def test_should_have_an_entries_association
        assert_equal 2, users(:gleb).entries.size
        assert_equal entries(:one), users(:gleb).entries.first
      end
    end
    This is the entries.yml
    Code:
    one:
      title: Entry one
      content: Day one
      user: gleb
    
    two:
      title: Entry two
      content: Day two
      user: gleb
    This is the error
    Code:
    Started
    ...F
    Finished in 0.059461 seconds.
    
      1) Failure:
    test_should_have_an_entries_association(UserTest) [/test/unit/user_test.rb:7]:
    <#<Entry id: 2053932785, title: "Entry one", content: "Day one", user_id: 2102312308, created_at: "2010-01-21 20:15:48", updated_at: "2010-01-21 20:15:48">> expected but was
    <#<Entry id: 298486374, title: "Entry two", content: "Day two", user_id: 2102312308, created_at: "2010-01-21 20:15:48", updated_at: "2010-01-21 20:15:48">>.
    
    4 tests, 5 assertions, 1 failures, 0 errors
    This is entries_controller.rb
    Code:
    class EntriesController < ApplicationController
      before_filter :login_required, :only => [ :new, :create ]
      
    
      def index
        @entry = Entry.find(:all)
      end
    
      def new
        @entry = Entry.new
      end
    
      def show
        @entry = Entry.find(params[:id])
      end
    
      def edit
        @entry = Entry.find(params[:id])
      end
    
      def update
        @entry = Entry.find(params[:id])
        @entry.update_attributes(params[:entry])
        render :action => 'show'
      end
    
      def create
        if session[:user_id]
        @entry = Entry.new(params[:entry])
        @current_user = User.find_by_id(session[:user_id])
        @entry.user_id = @current_user.login
              if @entry.save
                flash[:notice] = 'Entry was successfully created.'
              redirect_to(@entry) 
              end
        else
          redirect_to entries_path
        end
      end
    
      def destroy
        @entry = Entry.find(params[:id])
        @entry.destroy
    
        redirect_to(entries_path)
      end
    end

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    Cleveland, OH
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looking at the second line in the test code "assert_equal entriesone), usersgleb).entries.first" Using the .first method finds the first result that matches the search criteria. In this case the first entry with usergleb). If you look at the entries both have the same user, but the id's are different, with entry two having the smaller number, so in a normal search going in ascending order it would pop up first.

    Add order => "title") to the test, so it looks like

    assert_equal entriesone), usersgleb).entries.firstorder => "title")

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    Cleveland, OH
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops, the upside down smiley faces should be "(" followed by a ":" sorry 'bout that.

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    Cleveland, OH
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did it work?

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes worked fine thanks


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
  •