gpt4 book ai didi

ruby-on-rails - Rake 测试未在 minitest 中获取 capybara 测试

转载 作者:行者123 更新时间:2023-12-03 03:11:36 25 4
gpt4 key购买 nike

我正在设置一个基本模板,以便在 Rails 应用程序中进行 capybara 功能测试。我还使用 MiniTest 而不是 RSPEC。

运行 Rake Test 似乎没有进行我的功能测试。我在文件中有一个测试,运行 rake test 不会改变断言的数量。当我运行 rake 测试时,跳过测试也不会出现。

这是存储库的链接:https://github.com/rrgayhart/rails_template

这是我遵循的步骤

  1. 我将其添加到 Gemfile 并运行 bundle

    group :development, :test do
    gem 'capybara'
    gem 'capybara_minitest_spec'
    gem 'launchy'
    end
  2. 我将其添加到 test_helper

    require 'capybara/rails'
  3. 我创建了一个文件夹 test/features

  4. 我创建了一个名为 Drink_creation_test.rb 的文件

  5. 这是该功能测试文件中的代码

    require 'test_helper'

    class DrinkCreationTest < MiniTest::Unit::TestCase

    def test_it_creates_an_drink_with_a_title_and_body
    visit drinks_path
    click_on 'new-drink'
    fill_in 'name', :with => "PBR"
    fill_in 'description', :with => "This is a great beer."
    fill_in 'price', :with => 7.99
    fill_in 'category_id', :with => 1
    click_on 'save-drink'
    within('#title') do
    assert page.has_content?("PBR")
    end
    within('#description') do
    assert page.has_content?("td", text: "This is a great beer")
    end
    end

    end

我认为我遇到了无法正确连接某些内容的问题。如果我还可以提供任何其他信息来帮助诊断此问题,请告诉我。

最佳答案

这里发生了很多事情。首先,默认的 rake test 任务不会选取不在默认测试目录中的测试。因此,您需要移动测试文件或添加新的 rake 任务来测试 test/features 中的文件。

由于您正在使用 capybara_minitest_spec您需要在测试中包含 Capybara::DSLCapybara::RSpecMatchers 。由于您在此测试中没有使用 ActiveSupport::TestCase 或其他 Rails 测试类之一,因此您可能会发现数据库中存在不一致,因为该测试是在标准 Rails 测试事务之外执行的。

require 'test_helper'

class DrinkCreationTest < MiniTest::Unit::TestCase
include Capybara::DSL
include Capybara::RSpecMatchers

def test_it_creates_an_drink_with_a_title_and_body
visit drinks_path
click_on 'new-drink'
fill_in 'name', :with => "PBR"
fill_in 'description', :with => "This is a great beer."
fill_in 'price', :with => 7.99
fill_in 'category_id', :with => 1
click_on 'save-drink'
within('#title') do
assert page.has_content?("PBR")
end
within('#description') do
assert page.has_content?("td", text: "This is a great beer")
end
end

end

或者,您可以使用minitest-railsminitest-rails-capybara生成运行这些测试。

$ rails generate mini_test:feature DrinkCreation
$ rake minitest:features

关于ruby-on-rails - Rake 测试未在 minitest 中获取 capybara 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19800545/

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