# ANN: nu\_record = assert\_latest(Model){ create\_some\_model()

**URL:** <https://www.ruby-forum.com/t/ann-nu-record-assert-latest-model-create-some-model/108795>\
**Category:** Rails\
**Created:** [July 7, 2007, 5:07am UTC](https://www.ruby-forum.com/t/ann-nu-record-assert-latest-model-create-some-model/108795 "2007-07-07T05:07:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [July 7, 2007, 5:07am UTC](https://www.ruby-forum.com/t/ann-nu-record-assert-latest-model-create-some-model/108795/1 "2007-07-07T05:07:05Z")

</div>

Railsters:

A recent question here asked how to get the ID of a recently added Model  
object. (The answer is that methods like .create and .save populate the  
.id  
field.)

This leads to a new question: How can a test detect the most recently  
added  
record? The best way is for “unit test” code to directly call methods  
who  
create the record, and they should return it.

Sometimes that’s not so easy. Controller tests, for example, only return  
their @ instance variables in their assigns[] hash, and a controller  
that  
creates model objects might not have a good reason to assign and  
remember  
them.

To cover these situations (and to respond to several needs in my current  
project), I invented two new assertions:

- assert\_latest(Model, message = nil, &block)
- deny\_latest(Model, message = nil, &block)

Both take…

- Model - an ActiveRecord class, such as User, Chat, etc…
- message - optional string to prefix any flunk messages
- block - a required closure to call the tested code

The assert\_ passes, and the deny\_ fails, if any new record got created  
in  
that Model.

Here’s a test that checks we allow a user to sign up:

def test\_should\_allow\_signup  
user = assert\_latest User do  
create\_user(:login =\> ‘phlip’)  
assert\_response :redirect  
assert\_redirected\_to ‘[http://test.host/phlip](http://test.host/phlip)’  
end  
assert\_equal ‘phlip’, user.login  
end

(Heck - my programs had better allow /me/ to sign up!!)

And here’s code to prevent a bogus signup attempt from creating a new  
User  
record:

def test\_should\_require\_login\_on\_signup  
deny\_latest User do  
create\_user(:login =\> nil)  
assert assigns(:user).errors.on(:login)  
assert\_response :success  
end  
end

Below my sig is a bio-plugin. Simply copy it into your test\_helper.rb  
file,  
inside its main class there, and add assert\_latest and deny\_latest to  
every  
test that creates something - or should not!

Note that the code could not simply find the highest ID currently in use  
and  
simply add 1. A database table remembers an internal “Max ID” value, and  
always increments this. Other test cases may create and delete records;  
each  
bumps this “high water mark” up. So the implementation must find the  
lowest  
new ID that’s higher than the maximum stored in any record.

The committee would be interested to hear if any rare arcane databases  
out  
there fail this system.

–  
Phlip  
[http://www.oreilly.com/catalog/9780596510657/](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax

def assert\_latest(model, message = nil, &block )  
return get\_latest(model, &block) ||  
flunk\_latest(model, message, true)  
end

def deny\_latest(model, message = nil, &block)  
get\_latest(model, &block) and  
flunk\_latest(model, message, false)  
end

def get\_latest(model)  
max\_id = model.maximum(:id)  
yield

```
return model.find( :first,
                   :conditions => "id > #{max_id}",
                   :order => "id desc" )

```

end

def flunk\_latest(model, message, polarity)  
message = if message then “#{message}\n” else ‘’ end  
message += 'a new ’  
message += model.name  
message += ’ record should ’  
message += 'not ’ unless polarity  
message += ‘be created’  
flunk(message)  
end
