我连接了@rails/ujs,将其固定在js文件中。当我试图在开发服务器上执行时,我看到了代码如我所期望的那样工作。但是当我用selenium驱动程序启动capybara测试时,我在日志中看到了作为remote: true提交的表单(它有标志)。
这是我的代码:
views/articles/index.html.slim
= 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
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
class ArticlesController < ApplicationController
def index
@articles = Article.where(
'title LIKE :query OR body LIKE :query',
query: "%#{params[:query]}%"
)
endarticles/index.js.erb
articlesTable = '<%= j(render 'articles', articles: @articles) %>';
container = document.getElementById('articles')
container.innerHTML = articlesTablespec/acceptance/index_page_spec.rb
...
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不正确,但不确定,我到底错过了什么。有人能帮忙吗?非常感谢您提前!
发布于 2022-01-19 16:33:54
如果它在开发env中有效,但在test env中不起作用,那么您最有可能在一个JS文件中出现错误。在开发模式中,每个JS文件分别加载,因此其中一个文件中的错误不会影响其他JS文件。但是,在测试(和生产)模式下,文件是连接在一起的,这意味着一个JS文件中的错误可能会阻止其他JS文件的运行。检查浏览器日志在开发模式下的JS错误,并修复它们。
https://stackoverflow.com/questions/70769822
复制相似问题