首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails-ujs在Rails 7中的Capybara测试中不工作(使用Selenium驱动程序)

Rails-ujs在Rails 7中的Capybara测试中不工作(使用Selenium驱动程序)
EN

Stack Overflow用户
提问于 2022-01-19 11:30:25
回答 1查看 531关注 0票数 0

我连接了@rails/ujs,将其固定在js文件中。当我试图在开发服务器上执行时,我看到了代码如我所期望的那样工作。但是当我用selenium驱动程序启动capybara测试时,我在日志中看到了作为remote: true提交的表单(它有标志)。

这是我的代码:

views/articles/index.html.slim

代码语言:javascript
复制
= form_tag root_path, method: :get, remote: true, data: { controller: 'forms', forms_target: "form" }, id: :search_form  do
  = label_tag :query, t('artiles.search')
  = text_field_tag :query, nil, placeholder: t('articles.enter_your_search_query_here'),  data: { action: "input->forms#search" }
  = submit_tag t('buttons.find')

#articles
  = render 'articles', articles: @articles 

app/javascript/controllers/forms_controller.js

代码语言:javascript
复制
import { Controller } from "@hotwired/stimulus"
import Rails from "@rails/ujs";

export default class extends Controller {
  static targets = [ "form" ]

  search() {
    clearTimeout(this.timeout)
    this.timeout = setTimeout(() => {
      Rails.fire(this.formTarget, 'submit')
    }, 200)
  }
}

articles_controller.rb

代码语言:javascript
复制
class ArticlesController < ApplicationController
  def index
    @articles = Article.where(
      'title LIKE :query OR body LIKE :query',
      query: "%#{params[:query]}%"
    )
  end

articles/index.js.erb

代码语言:javascript
复制
articlesTable = '<%= j(render 'articles', articles: @articles) %>';

container = document.getElementById('articles')

container.innerHTML = articlesTable

spec/acceptance/index_page_spec.rb

代码语言:javascript
复制
...

  context 'when user fills search form with existing value', js: true do
    before do
      fill_in 'query', with: target_article.title
      find("input[name='commit']").click
    end

    it "User sees only target article on the page" do
      (articles - [target_article]).map(&:title).each do |title|
        expect(page).not_to have_text(title)
      end

      expect(page).to have_text(target_article.title)
    end
  end

...

当我在页面上启动一些js文件(例如发送AJAX请求)时,我在测试日志中看到它们,因此我看到selenium驱动程序可以工作。我想,我已经连接了rails-ujs不正确,但不确定,我到底错过了什么。有人能帮忙吗?非常感谢您提前!

EN

回答 1

Stack Overflow用户

发布于 2022-01-19 16:33:54

如果它在开发env中有效,但在test env中不起作用,那么您最有可能在一个JS文件中出现错误。在开发模式中,每个JS文件分别加载,因此其中一个文件中的错误不会影响其他JS文件。但是,在测试(和生产)模式下,文件是连接在一起的,这意味着一个JS文件中的错误可能会阻止其他JS文件的运行。检查浏览器日志在开发模式下的JS错误,并修复它们。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70769822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档