gpt4 book ai didi

ruby-on-rails-4 - Rails 4 Rspec expect{}.to change not registering 尽管测试按计划工作

转载 作者:行者123 更新时间:2023-12-04 06:55:41 25 4
gpt4 key购买 nike

我正在使用 Rails 4、RSpec、Capybara 和 selenium。我遇到了一个奇怪的问题,在过去一天左右的时间里,我似乎无法解决这个问题。我有一个相当基本的请求规范失败,因为测试没有注册对模型的更改。这是我的规范:

describe "manage pages", :js => true do
before :each do
@fake_page = FactoryGirl.create(:page_with_block, name: "Foobar")
@fake_page2 = FactoryGirl.create(:page_with_block, name: "sdfdf")
visit admin_pages_path
save_page
end

it "should delete an image when link is clicked" do
expect {
within "#page_#{@fake_page.id}" do
click_link 'Destroy'
end
wait = Selenium::WebDriver::Wait.new ignore: Selenium::WebDriver::Error::NoAlertPresentError
alert = wait.until { page.driver.browser.switch_to.alert }
alert.accept
}.to change(Page, :count).by(-1)
expect(page).to have_content "Listing Pages"
expect(page).not_to have_content "Foobar"
save_page
end
end

如您所见,这是对销毁和确认 js 警报测试的基本点击。问题是它总是失败:

......F

Failures:

1) Pages manage pages should delete an image when link is clicked
Failure/Error: expect {
expected #count to have changed by -1, but was changed by 0
# ./spec/requests/admin/pages_spec.rb:116:in `block (3 levels) in <top (required)>'

Finished in 11.74 seconds (files took 3.81 seconds to load)
7 examples, 1 failure

根据我之前放置的 save_page 文件,以及当我注释掉“expect.to change”时测试结束时的文件,一切都按预期工作,假页面是在之前,测试通过并单击销毁并接受警报,页面被销毁并重新加载索引页面而没有内容。我不明白的是为什么 Change 匹配器看不到它实际上在工作。

我读到它可能与我的 rails_helper.rb 文件有关,但我查找的所有内容都有类似的设置,即使不完全相同:

    ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"

config.use_transactional_fixtures = false

config.infer_spec_type_from_file_location!

config.include Capybara::DSL

config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end

config.after(:suite) do
FileUtils.rm_rf(Dir["#{Rails.root}/public/test/system/"])
end


end

由于我在谷歌或本网站上找不到其他类似情况,我一定是这里做错了什么。谁能指出我代码中的缺陷?提前致谢。

最佳答案

我一直在为同样的问题而苦苦挣扎。你等待逻辑可能有缺陷。我用了this approach对我来说效果很好。

从这篇文章中复制粘贴 - 基本上你需要创建一个助手:

# spec/support/wait_for_ajax.rb
module WaitForAjax
def wait_for_ajax
Timeout.timeout(Capybara.default_max_wait_time) do
loop until finished_all_ajax_requests?
end
end

def finished_all_ajax_requests?
page.evaluate_script('jQuery.active').zero?
end
end

RSpec.configure do |config|
config.include WaitForAjax, type: :feature
end

在您的 spec_helper.rb 中自动包含所有支持文件

spec/support/**/*.rb in our spec_helper.rb

或者像我一样手动操作:

#rails_helper.rb
require_relative 'support/wait_for_ajax'

现在我的测试通过了!

require 'rails_helper'

RSpec.feature "Call Me Back contact front feature spec >", :type => :feature, js: true do
feature 'valid form' do
scenario "with name and phone" do
visit '/'
execute_script '$("#call-popup").fadeIn(300)'

fill_in 'call_me_back_request_name', with: 'Clark Kent'
fill_in 'call_me_back_request_phone', with: '1234445566'

within '#new_call_me_back_request' do
expect{
find("input[type='submit']").click
wait_for_ajax #<- the magic is here!
}.to change(Request, :count).by(1)
end
end
end

关于ruby-on-rails-4 - Rails 4 Rspec expect{}.to change not registering 尽管测试按计划工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29973349/

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