简体   繁体   English

使用Javascript通过Test :: Unit和Capybara在Ruby on Rails中进行集成测试

[英]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: 使用RSpec和Capybara,我曾编写以下代码来测试有一些javascript正在进行的页面中的功能:

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? 现在,使用Test :: Unit和Capybara,我如何实现相同的结果? 'Cause when I do test 'checks some feature on some scenario', js: true it doesn't seem to work. '因为当我test 'checks some feature on some scenario', js: true它似乎不起作用。

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? 没有这样的样板代码还有其他解决方案吗?

Ok, I end up resolving that by defining a method in test_helper.rb that wraps the test within the capybara driver assigning: 好的,我最终通过在test_helper.rb中定义一个方法来解决这个问题,该方法将测试包装在capybara驱动程序中分配:

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. 我读到minitest数据可以在这里使用,但我没有进一步挖掘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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