Forum: Ruby on Rails Test Driven Design and Rails Helpers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-17 18:30
(Received via mailing list)
I have a bit of a problem trying to implement using TDD.  As I
understand it
the idea is to decide what you want the code to do, write a test to
check
that it does, then write the code and get it to pass the test.

I keep coming across a problem with this, here is an example:

What I want the code to do:
Show a link_to_remote on the page, referencing a particular url with
params
and a particular div.

I am using the built in rails test system.  As far as I can see the only
way
to check my link_to_remote is coded correctly is by using assert_select
to
check the html in the response.  This is easy enough to do but requires
knowledge of the html that link_to_remote generates.  So what I have to
do
is write the view first, containing the link to remote, check that the
link
works correctly, look at the html and write the test.

Is there an alternative?  Apologies if this has been discussed at length
elsewhere, I googled in vain.

Colin
72ea925c0ca3d19fdd2f12fa76681624?d=identicon&s=25 Stephan Wehner (stephanwehner)
on 2009-05-17 19:25
Colin Law wrote:
> I have a bit of a problem trying to implement using TDD.  As I
> understand it
> the idea is to decide what you want the code to do, write a test to
> check
> that it does, then write the code and get it to pass the test.
>
> I keep coming across a problem with this, here is an example:
>
> What I want the code to do:
> Show a link_to_remote on the page, referencing a particular url with
> params
> and a particular div.
>
> I am using the built in rails test system.  As far as I can see the only
> way
> to check my link_to_remote is coded correctly is by using assert_select
> to
> check the html in the response.  This is easy enough to do but requires
> knowledge of the html that link_to_remote generates.  So what I have to
> do
> is write the view first, containing the link to remote, check that the
> link
> works correctly, look at the html and write the test.
>
> Is there an alternative?  Apologies if this has been discussed at length
> elsewhere, I googled in vain.
>

Hey,

maybe if you start differently it will be easier:

1. First write a test for the action. (What should it do)
2. Then implement the action.
3. Then write a test for the link_remote_to placement within whichever
template/action-render
4. Then change the template to contain the link.

At point 3 you should know the form of the URL that is required.


Stephan

> Colin
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-17 22:59
(Received via mailing list)
2009/5/17 Stephan Wehner <rails-mailing-list@andreas-s.net>

> > What I want the code to do:
> > do
> maybe if you start differently it will be easier:
>
> 1. First write a test for the action. (What should it do)
> 2. Then implement the action.
> 3. Then write a test for the link_remote_to placement within whichever
> template/action-render
> 4. Then change the template to contain the link.
>
> At point 3 you should know the form of the URL that is required.
>
>
I think I have not explained myself adequately.  I have no problem with
points 1 and 2 - the test and the implementation for the action invoked
when
the remote call is made.  It is point 3 that is the problem.  How do I
write
the test for the link_to_remote placement unless I know the form of the
html
that link_to_remote generates?  I know the URL but not how the div and
url
appear within the Ajax.Updater javascript call.  In principle I do not
even
know that there is an Ajax.Updater call, nor whether I need to check any
of
the other parameters of the call.

The same problem appears with helpers such as datetime_select, until I
put
it on the page and look at how the individual select boxes are
configured,
how can I write the test to check that it has been initialised with the
correct data?

Colin
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-05-17 23:28
(Received via mailing list)
Colin Law wrote:

> I think I have not explained myself adequately.  I have no problem with
> points 1 and 2 - the test and the implementation for the action invoked
> when the remote call is made.  It is point 3 that is the problem.  How
> do I write the test for the link_to_remote placement unless I know the
> form of the html that link_to_remote generates?

Here is a bit of hemming and hawing, while I work on projects other than
assert_xhtml and assert_rjs_ (as seen at
http://c2.com/cgi/wiki?AssertXhtml )

TDD (aka BDD) works best on greenfield projects - ideally on platforms
which
were themselves invented via TDD. Such projects will have assertions
matching
each feature, ready to use. You simply alternate between writing
assertions and
writing code statements.

 > I know the URL but not
> how the div and url appear within the Ajax.Updater javascript call.  In
> principle I do not even know that there is an Ajax.Updater call, nor
> whether I need to check any of the other parameters of the call.

When TDDing into a legacy system, especially a View, expect to cheat
early and
often.

Write the test up until the point where you have no assertion, then
fudge it.
Assert that your <a onclick> matches 42, for example.

Write the code you want, get the assertion to fail, and look at the
diagnostic.
Copy its relevant details into your assert_match, and keep going. A
change in
the RJS library might someday break your assertion; you don't care.

The _next_ time you TDD an <a onclick>, clone that assertion and upgrade
it. You
will soon discover an application-specific assertion pattern which might
work
for all Ajax in the world, but will tolerably propel your own project.

My assert_rjs_ is teetering on the brink of doing this:

   a = assert_xhtml{|x| x.a.my_id! }
   assert_rjs_ a[:onclick], :remote_function, :url => { :action => 'yo'
}

However, these days I am shamelessly earning money instead of working on
it.
Sorry! (-:

--
   Phlip
   http://flea.sourceforge.net/resume.html
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-19 10:10
(Received via mailing list)
OK, thanks, that is pretty much what I have been doing.  I thought there
might be a better way.
Colin

2009/5/17 Phlip <phlip2005@gmail.com>
This topic is locked and can not be replied to.