# Testing javascript

**URL:** <https://www.ruby-forum.com/t/testing-javascript/171977>\
**Category:** Rails\
**Created:** [July 12, 2009, 9:02pm UTC](https://www.ruby-forum.com/t/testing-javascript/171977 "2009-07-12T21:02:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [July 12, 2009, 9:02pm UTC](https://www.ruby-forum.com/t/testing-javascript/171977/1 "2009-07-12T21:02:51Z")

</div>

Hi,

I have two select lists in my app called ‘topic’ and ‘member’.  
Based on what I select in topic, member is populated accordingly (using  
java script).  
eg. If I select ‘subject1’ in topic, member should be populated with the  
values ‘prof x’, ‘prof y’ and ‘prof z’.  
Can I test that my java script function is populating member with the  
correct values in a unit test?

So far I have tried:

def test\_correct\_value\_returned  
applicant = Applicant.new(valid\_attributes(:topic =\> “”, :member =\>  
“”,))  
applicant.topic = “subject1”  
assert\_equal [“prof x”, “prof y”, “prof z”], applicant.member  
end

But I get:

1. Failure:  
test\_test\_correct\_value\_returned(ApplicantTest)  
[/test/unit/applicant\_test.rb:2098]:  
\<[“prof x”, “prof y”, “prof z”]\> expected but was  
\<""\>.

Can anyone give me a pointer as to how I could go about getting this to  
work?  
Thanks very much.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [July 12, 2009, 9:17pm UTC](https://www.ruby-forum.com/t/testing-javascript/171977/2 "2009-07-12T21:17:52Z")

</div>

On Jul 12, 8:02 pm, Jim B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi,
> 
> I have two select lists in my app called ‘topic’ and ‘member’.  
> Based on what I select in topic, member is populated accordingly (using  
> java script).  
> eg. If I select ‘subject1’ in topic, member should be populated with the  
> values ‘prof x’, ‘prof y’ and ‘prof z’.  
> Can I test that my java script function is populating member with the  
> correct values in a unit test?

Not really, since your ruby code doesn’t know how to execute  
javascript. (I often use the Test.Unit stuff from prototype to write  
javascript unit tests)

Fred

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [July 14, 2009, 9:12am UTC](https://www.ruby-forum.com/t/testing-javascript/171977/3 "2009-07-14T09:12:52Z")

</div>

> Not really, since your ruby code doesn’t know how to execute  
> javascript. (I often use the Test.Unit stuff from prototype to write  
> javascript unit tests)

Thanks for that Fred.  
It’s good to know that I was barking up the wrong tree.  
Since reading your post I have been looking at the javascript\_test  
plugin but still unsure as to how to write a sensible test with it.

I guess I’ll rephrase my question and post it again.
