Assert_equal behavior

I am testing with assert_equal

Here is my Fixture:
bob1:
id: 1000001
login: bob
glogin: [email protected]
session_token: 77a38 # test

Here is my Test method:
class Guser < ActiveRecord::Base
attr_accessor :session_token
def self.match_login(glogin, login, ses_token)
u=find(:first, :conditions=>[“glogin = ? AND login = ?”, glogin,
login])
return nil if u.nil?
if (u.session_token != ses_token)
u.session_token = ses_token
return u
end
nil
end
end

The test database table is Guser and it has no rows to start with

Here is my where I call test method:
assert_equal @bob1, Guser.match_login(“[email protected]”, “bob”, “77a038”)

I get this message on the console:

  1. Failure:
    test_match_login(GuserTest) [test/unit/guser_test.rb:14]:
    <#<Guser id: 1000001, glogin: “[email protected]”, login: “bob”,
    session_token: “77a
    0d943cdbace52716a9ef9fae12e45e2788d38”, created_at: “2010-02-02
    15:45:37”, updat
    ed_at: “2010-02-02 15:45:37”>> expected but was
    .
    1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

I expected the test to fail as there was no record in the database and
the ‘find’ should return nothing. But it creates a new one and the test
is a success. I do not see any code for creating new record or save. So
why is it creating a new record?
I do not understand assert_equal behavior , can someone please explain?

On Feb 2, 3:58 pm, Vishwa R. [email protected] wrote:

I expected the test to fail as there was no record in the database and
the ‘find’ should return nothing. But it creates a new one and the test
is a success. I do not see any code for creating new record or save. So
why is it creating a new record?
I do not understand assert_equal behavior , can someone please explain?

assert_equal is a simple beast - it just compares the two arguments
you pass to it. What you actually don’t understand is how the test
database is setup. In particular if you have a fixtures file for a
table (as you appear to do) rails will create rows in that table for
you based on your fixtures file.

Fred

On Feb 2, 9:58 am, Vishwa R. [email protected] wrote:

class Guser < ActiveRecord::Base
end
<#<Guser id: 1000001, glogin: “[email protected]”, login: “bob”,
session_token: “77a
0d943cdbace52716a9ef9fae12e45e2788d38”, created_at: “2010-02-02
15:45:37”, updat
ed_at: “2010-02-02 15:45:37”>> expected but was
.
1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

You want users(:bob1), not @bob1, in your test (unless you’ve
explicitly turned on use_instantiated_fixtures = true in your
test_helper.rb).

Jeff

purpleworkshops.com