# Test Driven Design and Rails Helpers

**URL:** <https://www.ruby-forum.com/t/test-driven-design-and-rails-helpers/168178>\
**Category:** Rails\
**Created:** [May 17, 2009, 6:30pm UTC](https://www.ruby-forum.com/t/test-driven-design-and-rails-helpers/168178 "2009-05-17T18:30:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [May 17, 2009, 6:30pm UTC](https://www.ruby-forum.com/t/test-driven-design-and-rails-helpers/168178/1 "2009-05-17T18:30:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [May 17, 2009, 7:25pm UTC](https://www.ruby-forum.com/t/test-driven-design-and-rails-helpers/168178/2 "2009-05-17T19:25:27Z")

</div>

Colin L. 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

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [May 17, 2009, 10:59pm UTC](https://www.ruby-forum.com/t/test-driven-design-and-rails-helpers/168178/3 "2009-05-17T22:59:04Z")

</div>

2009/5/17 Stephan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > 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

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [May 17, 2009, 11:28pm UTC](https://www.ruby-forum.com/t/test-driven-design-and-rails-helpers/168178/4 "2009-05-17T23:28:56Z")

</div>

Colin L. 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](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 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 , 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](http://flea.sourceforge.net/resume.html)

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [May 19, 2009, 10:10am UTC](https://www.ruby-forum.com/t/test-driven-design-and-rails-helpers/168178/5 "2009-05-19T10:10:21Z")

</div>

OK, thanks, that is pretty much what I have been doing. I thought there  
might be a better way.  
Colin

2009/5/17 Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
