Test won't work

Can anbody explain why this test doesn’t work?

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

one:
  title: Entry one
  content: Day one
  user: gleb

two:
  title: Entry two
  content: Day two
  user: gleb

This is the error

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

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

Looking at the second line in the test code “assert_equal entries(:one), users(:gleb).entries.first” Using the .first method finds the first result that matches the search criteria. In this case the first entry with user(:gleb). 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 entries(:one), users(:gleb).entries.first(:order => “title”)

Oops, the upside down smiley faces should be “(” followed by a “:” sorry 'bout that.

Thanks

Did it work?

Yes worked fine thanks