Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing JavaScript views in Rails

What is the best way to test JavaScript views (test to see if a page works JavaScript wise) in Rails?

So lets say I have a page called /users/new that contains a form that does something. I would like to have it so that I submit the form and the JavaScript testing tool will let me know if that page breaks or not. What is the best way to do this?

I have come across three options for testing:

  1. Konacha (best tool so far for JS BDD) http://www.solitr.com/blog/2012/04/konacha-tutorial-javascript-testing-with-rails/

  2. JasmineRice (Jasmine + Rails + Guard) https://github.com/bradphelan/jasminerice/

  3. Capybara Webkit. https://github.com/thoughtbot/capybara-webkit

The first two are basically there for BDD and isolated testing. The last one is more for what I'm looking for, but I don't want to have a separate testing setup for JavaScript BDD and integration testing.

Does anyone have a better solution?

like image 707
matsko Avatar asked Jul 03 '12 22:07

matsko


2 Answers

Check the Teaspoon Project on github.

https://github.com/modeset/teaspoon

It supports Mocha, Jasmine, and QUnit.. has nice support for running your specs headlessly with PhantomJS (or Selenium Webdriver), and allows using the full Rails asset pipeline (coffeescript, fixtures, etc).

Also, it was heavily influenced by my experience with other test runners using Rails.

like image 78
jejacks0n Avatar answered Nov 19 '22 20:11

jejacks0n


If you are using RSpec, you can use Capybara in an RSpec integration test:

spec/requests/my_spec.rb:

describe "my test", :js => true do
  it "should do something" do
    visit '/some/path'

    click_on 'Submit'

    page.should have_content 'Congratulations!'
  end
end

Remember to set Capybara.javascript_driver = :webkit in spec_helper.rb.

like image 40
Tanzeeb Khalili Avatar answered Nov 19 '22 20:11

Tanzeeb Khalili