# Asserting PUT methods

**URL:** <https://www.ruby-forum.com/t/asserting-put-methods/174489>\
**Category:** Rails\
**Created:** [August 27, 2009, 1:42pm UTC](https://www.ruby-forum.com/t/asserting-put-methods/174489 "2009-08-27T13:42:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jarl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jarl](https://www.ruby-forum.com/u/jarl)\
**Post date:** [August 27, 2009, 1:42pm UTC](https://www.ruby-forum.com/t/asserting-put-methods/174489/1 "2009-08-27T13:42:53Z")

</div>

Hi.

In my view I have a line like this:

link\_to(‘Update timestamp’, order\_path(@order, :update\_timestamp =\>  
true), :method =\> :put

This generates HTML like this  
[Update timestamp](https://www.ruby-forum.com/orders/2131771394?update_timestamp=true)

Basically it creates a normal link, which is overridden by a  
javascript function to send a (simulated) put request in stead, that  
is all fine, however I would like to create test that asserts that  
this javascript is there.

Basically I would like to create a test that fails if my view code was  
like this:  
link\_to(‘Update timestamp’, order\_path(@order, :update\_timestamp =\>  
true)

that is _without_ the ‘:method =\> :put’ as html\_options  
If I just have a standard assert, like  
assert\_select “a[href=?]”, order\_path(order, :update\_timestamp =\> true)  
it succeeds even if ‘:method =\> :put’ is not there.

Does anyone have a good idea of how to assert this.

Jarl

---

<div class="post-metadata">

**Author:** ![jarl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jarl](https://www.ruby-forum.com/u/jarl)\
**Post date:** [August 27, 2009, 10:47pm UTC](https://www.ruby-forum.com/t/asserting-put-methods/174489/2 "2009-08-27T22:47:18Z")

</div>

On Aug 27, 7:26 am, Jarl F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> javascript function to send a (simulated) put request in stead, that  
> it succeeds even if ‘:method =\> :put’ is not there.
> 
> Does anyone have a good idea of how to assert this.
> 
> Jarl

You can do something like assert\_select “a[onclick=?]”, //

Best Regards,  
Jason S.

---

<div class="post-metadata">

**Author:** ![jarl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@jarl](https://www.ruby-forum.com/u/jarl)\
**Post date:** [August 28, 2009, 10:12am UTC](https://www.ruby-forum.com/t/asserting-put-methods/174489/3 "2009-08-28T10:12:12Z")

</div>

Jason S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> You can do something like assert\_select “a[onclick=?]”, //

Yes, for now I have made like this:  
assert\_select “a#{PUT}[href=?]”, order\_path(order, :update\_timestamp =\>  
true)

and I have declared PUT in test\_helper like this:  
PUT = “[onclick="var f = document.createElement(‘form’);  
f.style.display = ‘none’; this.parentNode.appendChild(f); f.method =  
‘POST’; f.action = this.href;var m = document.createElement(‘input’);  
m.setAttribute(‘type’, ‘hidden’); m.setAttribute(‘name’, ‘\_method’);  
m.setAttribute(‘value’, ‘put’); f.appendChild(m);f.submit();return  
false;"]”

Fortunately this PUT value is a constant because it refers to  
‘this.href’, but my tests will break if any other javascript is  
generated (in newer version of rails) that has the same functionality,  
but differs in just a single space. I hoped to have some kind of test  
that the execution of the javascript would result in a HTTP PUT  
request.

Jarl
