简体   繁体   中英

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

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

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.

本文推荐Akephalos

I have used cucumber and capybara with selenium. This was very frustrating because selenium did not seem to be able to see dynamically generated javascript, despite the fact that capybara was supposed to be waiting for it. That was in January 2011. Things may be different now.

Currently, I am using cucumber and capybara with akephalos. So far, it has been very difficult because 1. it is headless, so you can't see progress. Capybara's "save_and_open" call has helped to a degree. 2. jQuery and akephalos don't seem to play that nicely together. For instance, triggering on a radio button with jquery's .change() works fine in chrome, but doesn't in akephalos. Maybe this is intentional because I later heard somewhere that it doesn't work in IE. I fixed the issue by using .click() instead of .change() for the radio button but since the .change function was set up to run on a bunch of questions, I had to code specifically to get it to work for the test.

The bottom line for me is that automated javascript acceptance testing in a rails env is still immature and possibly more work that it is worth.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM