Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript integration testing in Ruby on Rails

I've searched a bit for this and tried to implement a self-made solution but so far haven't found to be confident with it.

What I need is to write integration tests in Ruby on Rails which interact with JavaScript and get programmatic ways to assert some behaviors. I'm using Test::Unit for the controller/models part but I'm struggling to test some jQuery/JavaScript behaviors used by my app. Mainly it consists in ajax calls and interactions in the UI which updates some sets of information.

I haven't found a solution which makes me confident and which integrates nicely with autotest and the whole red-green process, so for now most parts of my client-side code is untested and that's making me nervous (as it should be :P).

So, does anyone have suggestions for best practices on this issue? Unit testing JS is a bit tricky, as Crockford points out, because it dependes heavily on the current state of the UI and etc and as AFAIK even he hasn't found a good way to implement decent testing...

Shortly: I need to implement tests for some UI behavior which depends on Ajax, integrating with autotest or some other CI tool and haven't found a good and elegant way to do it.

Thanks all for the attention, Best Regards

like image 463
victorcampos Avatar asked Jan 30 '11 17:01

victorcampos


3 Answers

AFAIK, outside of a combination of Capybara with Selenium Web-Driver there is very few options for automated testing of JS code. I use cucumber with capybara and selenium web-driver and because selenium-webdriver actually launches firefox or chrome to go through testing a particular page with ajax call, It does take significantly longer to run through a suite of tests.

There are some alternatives but they dont work all the time or for every situations. For instance: Capybara with envjs

like image 197
Vlad Gurovich Avatar answered Nov 03 '22 20:11

Vlad Gurovich


In April 2011 the thoughtbot guys updated their quest for javascript testing. Akephalos has fallen out of favor for the following reasons:

Bugs: as previously mentioned, there are bugs in htmlunit, specifically with jQuery’s live. Although all browser implementations have bugs, it’s more useful if tests experience the same bugs as actual browsers.

Compatibility: htmlunit doesn’t fully implement the feature set that modern browsers do. For example, it doesn’t fully handle DOM ranges or Ajax file uploads.

Rendering: htmlunit doesn’t actually render the page, so tests that depend on CSS visibility or positioning won’t work.

Performance: when most of your tests use Javascript, test suites with htmlunit start to crawl. It takes a while to start up a test with Akephalos, and a large test suite can easily take 10 or 15 minutes.

So they rolled their own solution which is open source - capybara-webkit. It's still fairly new but it looks like the way to go now.

like image 38
Dty Avatar answered Nov 03 '22 19:11

Dty


This article recommends Akephalos.

like image 2
zetetic Avatar answered Nov 03 '22 21:11

zetetic