# Strange behaviour in Unit Test

**URL:** <https://www.ruby-forum.com/t/strange-behaviour-in-unit-test/84975>\
**Category:** Rails\
**Created:** [February 8, 2007, 11:02pm UTC](https://www.ruby-forum.com/t/strange-behaviour-in-unit-test/84975 "2007-02-08T23:02:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e8c25b/32.png) [@Joram](https://www.ruby-forum.com/u/Joram)\
**Post date:** [February 8, 2007, 11:02pm UTC](https://www.ruby-forum.com/t/strange-behaviour-in-unit-test/84975/1 "2007-02-08T23:02:20Z")

</div>

Hi,

I’m having a weird problem in one of my unit tests.

this code generates an assertion error on the second line

assert !@valid\_category.update\_attributes(:name =\> nil)  
assert\_equal old\_name, @valid\_category.name

However, when I write the following, the error is no more…

assert !@valid\_category.update\_attributes(:name =\> nil)  
@valid\_category.reload  
assert\_equal old\_name, @valid\_category.name

This sure can’t be normal? I tried to write out the name of the  
@valid\_category between the 2 lines, and its empty.

Another question:  
Is it correct that the update\_attribute method bypasses validation?  
According to the API this is the fact, but why would that be, updates  
should also be valid??

Thanks!

Joram

---

<div class="post-metadata">

**Author:** ![Joram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e8c25b/32.png) [@Joram](https://www.ruby-forum.com/u/Joram)\
**Post date:** [February 8, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/strange-behaviour-in-unit-test/84975/2 "2007-02-08T23:07:20Z")

</div>

That is normal behavior. #update\_attributes actually does update the  
attributes, but returns on whether the save to the database was  
successful  
or not. One reason it’s done this way is to allow for field re-filling  
on  
forms when there are errors in the form.

Jason

---

<div class="post-metadata">

**Author:** ![Joram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e8c25b/32.png) [@Joram](https://www.ruby-forum.com/u/Joram)\
**Post date:** [February 8, 2007, 11:08pm UTC](https://www.ruby-forum.com/t/strange-behaviour-in-unit-test/84975/3 "2007-02-08T23:08:20Z")

</div>

Sorry, hit the send button too quickly. As to your second question,  
update\_attributes does not bypass validations. Nothing in AR bypasses  
the  
validations unless you specifically tell it to do so.

Jason

---

<div class="post-metadata">

**Author:** ![Joram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e8c25b/32.png) [@Joram](https://www.ruby-forum.com/u/Joram)\
**Post date:** [February 8, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/strange-behaviour-in-unit-test/84975/4 "2007-02-08T23:17:21Z")

</div>

Thanks for the information, Jason, it does make sense to me now.

About the validation, I found this in the API:

"update\_attribute(name, value)

Updates a single attribute and saves the record. This is especially  
useful for boolean flags on existing records. Note: This method is  
overwritten by the Validation module that’ll make sure that updates  
made with this method doesn’t get subjected to validation checks.  
Hence, attributes can be updated even if the full object isn’t valid.  
"

I’m getting an MySql exception when I use this method, stating that I  
want to insert a null value into a column where null is not allowed.

---

<div class="post-metadata">

**Author:** ![Joram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e8c25b/32.png) [@Joram](https://www.ruby-forum.com/u/Joram)\
**Post date:** [February 8, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/strange-behaviour-in-unit-test/84975/5 "2007-02-08T23:31:22Z")

</div>

> I’m getting an MySql exception when I use this method, stating that I  
> want to insert a null value into a column where null is not allowed.

Yes, update\_attribute does bypass validation. You are getting an  
error because of a null restriction on the column in MySQL. If it  
wasn’t for that the nil name would have persisted. Only use update  
attribute if you don’t want validations to run.

Aaron

---

<div class="post-metadata">

**Author:** ![Joram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e8c25b/32.png) [@Joram](https://www.ruby-forum.com/u/Joram)\
**Post date:** [February 8, 2007, 11:25pm UTC](https://www.ruby-forum.com/t/strange-behaviour-in-unit-test/84975/6 "2007-02-08T23:25:22Z")

</div>

> assert !@valid\_category.update\_attributes(:name =\> nil)  
> assert\_equal old\_name, @valid\_category.name

When an update fails validation the changes that you made are still in  
memory, they just don’t persist to the database. Think about when you  
fill out a form, click submit, and an validation error occurs. All  
the values you entered are returned and populate the form so you don’t  
have to enter them again.

@valid\_category.valid? =\> tells you if the current version is valid  
puts @valid\_category.errors.inspect =\> to see the validation errors

You can even do something like this to check that the error you  
expected occurred:  
assert\_equal( 1, @valid\_category.errors.length )  
assert\_equal( ‘name must have a value’,  
@valid\_category.errors.on(:name))

> assert !@valid\_category.update\_attributes(:name =\> nil)  
> @valid\_category.reload  
> assert\_equal old\_name, @valid\_category.name

This works because reload does exactly that, it reloads  
@valid\_category with the version from the database. Your  
update\_attributes failed validation so the database version was left  
unchanged.

Aaron

---

<div class="post-metadata">

**Author:** ![Joram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e8c25b/32.png) [@Joram](https://www.ruby-forum.com/u/Joram)\
**Post date:** [February 8, 2007, 11:38pm UTC](https://www.ruby-forum.com/t/strange-behaviour-in-unit-test/84975/7 "2007-02-08T23:38:23Z")

</div>

Thank you Aaron and Jason for your quick reply!

It’s all clear to me now 🙂
