# Testing page.alert

**URL:** <https://www.ruby-forum.com/t/testing-page-alert/139693>\
**Category:** Rails\
**Created:** [June 6, 2008, 5:06am UTC](https://www.ruby-forum.com/t/testing-page-alert/139693 "2008-06-06T05:06:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tamc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f1d935/32.png) [@tamc](https://www.ruby-forum.com/u/tamc)\
**Post date:** [June 6, 2008, 5:06am UTC](https://www.ruby-forum.com/t/testing-page-alert/139693/1 "2008-06-06T05:06:33Z")

</div>

Hello,

Testing newbie here. I have some AJAX code that would render an error  
message back to the host using page.alert. How would I test that?  
Personally I use RSpec, but Test::Unit is also fine. Thanks.

# If some error arises

render :update do |page|  
page.alert(“Error message!”)  
end

---

<div class="post-metadata">

**Author:** ![tamc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f1d935/32.png) [@tamc](https://www.ruby-forum.com/u/tamc)\
**Post date:** [July 4, 2008, 7:45pm UTC](https://www.ruby-forum.com/t/testing-page-alert/139693/2 "2008-07-04T19:45:15Z")

</div>

On Thu, Jun 5, 2008 at 8:13 PM, Thomas C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> Here’s how you can do it with rspec:

describe “some action” do  
it “should alert with Error message!” do  
page = mock(“page”)  
page.should\_receive(:alert).with(“Error message!”)  
controller.expect\_render(:update).and\_yield(page)  
get :some\_action  
end  
end

Here’s how I _think_ (haven’t tried it, but should work) you can do it  
with test/unit and mocha:

def test\_alert\_with\_error\_messaged  
page = mock()  
page.expects(:alert).with(“Error message!”)  
@controller.expects(:update).yields(page)  
get :some\_action  
end

Cheers,  
David
