# Assert\_equal behavior

**URL:** <https://www.ruby-forum.com/t/assert-equal-behavior/182769>\
**Category:** Rails\
**Created:** [February 2, 2010, 4:58pm UTC](https://www.ruby-forum.com/t/assert-equal-behavior/182769 "2010-02-02T16:58:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vihrao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f07891/32.png) [@vihrao](https://www.ruby-forum.com/u/vihrao)\
**Post date:** [February 2, 2010, 4:58pm UTC](https://www.ruby-forum.com/t/assert-equal-behavior/182769/1 "2010-02-02T16:58:34Z")

</div>

I am testing with assert\_equal

Here is my Fixture:  
bob1:  
id: 1000001  
login: bob  
glogin: removed\_email\_address@domain.invalid  
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(“removed\_email\_address@domain.invalid”, “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: “removed\_email\_address@domain.invalid”, 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?

---

<div class="post-metadata">

**Author:** ![vihrao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f07891/32.png) [@vihrao](https://www.ruby-forum.com/u/vihrao)\
**Post date:** [February 2, 2010, 7:34pm UTC](https://www.ruby-forum.com/t/assert-equal-behavior/182769/2 "2010-02-02T19:34:00Z")

</div>

On Feb 2, 3:58 pm, Vishwa R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![vihrao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f07891/32.png) [@vihrao](https://www.ruby-forum.com/u/vihrao)\
**Post date:** [February 2, 2010, 10:23pm UTC](https://www.ruby-forum.com/t/assert-equal-behavior/182769/3 "2010-02-02T22:23:47Z")

</div>

On Feb 2, 9:58 am, Vishwa R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Guser \< ActiveRecord::Base  
> end  
> \<#\<Guser id: 1000001, glogin: “removed\_email\_address@domain.invalid”, 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](http://purpleworkshops.com)
