Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration tests with Javascript through Test::Unit and Capybara in Ruby on Rails

With RSpec and Capybara, I used to write the following code to test for features in pages that had some javascript going on:

feature "Some Feature" do
    scenario "testing some scenario", js: true do
        # code
    end
end

Now, using Test::Unit with Capybara, how do I achieve the same result? 'Cause when I do test 'checks some feature on some scenario', js: true it doesn't seem to work.

EDIT

So, I was able to workaround with the following:

setup do
    Capybara.current_driver = Capybara.javascript_driver
end

teardown do
    Capybara.current_driver = Capybara.default_driver
end

test 'checks some feature on some scenario with javascript goin on' do
    # code
end

Is there any other solution without such a boilerplate code?

like image 706
dferrazm Avatar asked Jun 10 '26 07:06

dferrazm


1 Answers

Ok, I end up resolving that by defining a method in test_helper.rb that wraps the test within the capybara driver assigning:

def js
    Capybara.current_driver = Capybara.javascript_driver
    yield
    Capybara.current_driver = Capybara.default_driver
end

And using it like:

test 'checks some feature on some scenario with javascript goin on' do
    js do
        # code
    end
end

I read that minitest-data could be of use here, but I didn't dig out any further.

like image 86
dferrazm Avatar answered Jun 12 '26 22:06

dferrazm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!