gpt4 book ai didi

javascript - Rspec 和 javascript

转载 作者:行者123 更新时间:2023-11-27 22:58:36 26 4
gpt4 key购买 nike

在我的 ruby​​ 应用程序中,我有一个带有一些 javascript 函数的页面,负责填写表单字段。问题是,当我尝试使用 rspec 对此页面进行测试时,javascript 似乎没有运行,导致表单上出现错误。

有什么办法可以解决这个问题吗?

编辑(使用 Fred 建议)

我的spec_helper.rb 文件如下所示:

require 'capybara/rspec'
require 'factory_girl_rails'
require 'capybara/poltergeist'

require 'support/mailer_macros'
require 'support/test_helper'

Capybara.javascript_driver = :poltergeist

RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end

config.include FactoryGirl::Syntax::Methods

config.include Capybara::DSL

config.include(MailerMacros)
config.include(TestHelper)
config.before(:each) { reset_email }

config.expect_with :rspec do |c|
c.syntax = [:should, :expect]
end

end

我的测试如下所示:

require 'rails_helper'

RSpec.describe "Courses", type: :request do
let(:user) { FactoryGirl.create(:user) }

it "should create, show, edit and delete a course" do
login user
visit course_types_path("en")
fill_in "course_type_name", :with => "driving"
click_button "Add"
visit courses_path("en")
current_path.should eq(courses_path("en"))
click_link "Create new course"
current_path.should eq(new_course_path("en"))
select "driving", from: "course_course_type_id"
fill_in "course_name", :with => "course 1"
fill_in "course_price", :with => "12"
click_link "add_date_link"
select "John Doe", from: "course_teacher_id"
fill_in "course_max_students", :with => "1"
fill_in "course_address", :with => "Rua Adriano Correia de Oliveira A, Laranjeiro, Portugal"
click_button "Create new course"
current_path.should eq(courses_path("en"))
page.should have_content("New course created successfully")
page.should have_content("course 1")
end
end

什么时候改变它“应该创建、显示、编辑和删除类(class)”,js: true do

其他测试,在开始之前工作无法通过,并且当单击链接时页面似乎没有转到正确的位置(改为转到根目录),并且此测试未通过给出: ActionController::RoutingError:没有路由匹配[GET]“/fonts/fontawesome-webfont.woff”

最佳答案

要实现您想要的目标,您需要 capybara , poltergeist和 phantomJS。安装时poltergeist会自动下载phantomJS。

相关:http://www.railsonmaui.com/tips/rails/capybara-phantomjs-poltergeist-rspec-rails-tips.html

关于javascript - Rspec 和 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37350845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com