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 <a href="/orders/2131771394?update_timestamp=true" 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;">Update timestamp</a> 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
On Aug 27, 7:26 am, Jarl Friis <j...-VtTiYveqpzg@public.gmane.org> wrote:> 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 > <a href="/orders/2131771394?update_timestamp=true" 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;">Update timestamp</a> > > 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. > > JarlYou can do something like assert_select "a[onclick=?]", /<regexp here>/ Best Regards, Jason Stewart
Jason Stewart <jason.m.stewart-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:> You can do something like assert_select "a[onclick=?]", /<regexp > here>/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