使用cucumber测试时sql错误无法在事务内启动事务

2024-01-07

我是黄瓜新手,正在学习 BDD

当我尝试填写表单并创建记录时,会显示此 sqlite 错误,尽管当我在浏览器中手动尝试代码时没有错误。

我正在使用 Rails 4。

这是我的控制器代码

class Admin::ItemsController < ApplicationController
    def index
        @items=Item.all
    end

    def new
        @item=Item.new
    end

    def create
        @item=Item.new items_params
            respond_to do |format|
                if @item.save
                    format.html { redirect_to admin_items_path }
                else
                    format.html { redirect_to new_admin_items_path }
                end
            end
    end

private
    def items_params
        params.require(:item).permit(:name,:price)      
    end
end

这是我的功能文件

Feature: Manage Items
  In order to make a store
  As an admin
  I want to create and manage items

  Scenario: Items List
    Given I go to the new admin item page
    And I fill in "Name" with "Shampoo"
    And I fill in "Price" with "50"
    When I press "Create"
    Then I should be on the admin items page
    And I should see "Shampoo"

和步骤定义

require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths"))

Given /^I go to the (.+)$/ do |page_name|
  visit path_to(page_name)
end

And /^I fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
  fill_in(field.gsub(' ', '_'), :with => value)
end

When /^I press "([^\"]*)"$/ do |button|
  click_button(button)
end

Then /^I should be on the (.+)$/ do |page_name|
  current_path.should == path_to(page_name)
end

And /^I should see "(.*?)"$/ do |arg1|
  page.should have_content(arg1)
end

此错误显示在步骤 4 中,即单击“创建”按钮并调用 items#create 时。

我不知道代码有什么问题,希望有人能帮忙。

UPDATE:

我想问题出在 cucumber-rails 中的弃用https://github.com/cucumber/cucumber-rails/issues/231 https://github.com/cucumber/cucumber-rails/issues/231

完整轨迹:

Using the default profile...
WARNING: Nokogiri was built against LibXML version 2.7.8, but has dynamically loaded 2.9.0
DEPRECATION WARNING: ActionController::Integration is deprecated and will be removed, use ActionDispatch::Integration instead. (called from <module:Rails> at /home/phanindra/.gem/ruby/1.9.1/gems/cucumber-rails-1.3.0/lib/cucumber/rails/world.rb:9)
DEPRECATION WARNING: ActionController::IntegrationTest is deprecated and will be removed, use ActionDispatch::IntegrationTest instead. (called from <module:Rails> at /home/phanindra/.gem/ruby/1.9.1/gems/cucumber-rails-1.3.0/lib/cucumber/rails/world.rb:9)
Feature: Manage Items
  In order to make a store
  As an admin
  I want to create and manage items

  Scenario: Items List                       # features/manage_items.feature:6
DEPRECATION WARNING: #increment_open_transactions is deprecated and has no effect. (called from start at /home/phanindra/.gem/ruby/1.9.1/gems/database_cleaner-0.9.1/lib/database_cleaner/active_record/transaction.rb:11)
    Given I go to the new admin item page    # features/step_definitions/item_steps.rb:3
    And I fill in "Name" with "Shampoo"      # features/step_definitions/item_steps.rb:7
    And I fill in "Price" with "50"          # features/step_definitions/item_steps.rb:7
    When I press "Create"                    # features/step_definitions/item_steps.rb:11
      SQLite3::SQLException: cannot start a transaction within a transaction: begin transaction (ActiveRecord::StatementInvalid)
      /home/phanindra/rails/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:353:in `block in begin_db_transaction'
      /home/phanindra/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:305:in `block in log'
      /home/phanindra/rails/activesupport/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
      /home/phanindra/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:300:in `log'
      /home/phanindra/rails/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:353:in `begin_db_transaction'
      /home/phanindra/rails/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb:129:in `initialize'
      /home/phanindra/rails/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb:17:in `new'
      /home/phanindra/rails/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb:17:in `begin'
      /home/phanindra/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:231:in `begin_transaction'
      /home/phanindra/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:208:in `within_new_transaction'
      /home/phanindra/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
      /home/phanindra/rails/activerecord/lib/active_record/transactions.rb:209:in `transaction'
      /home/phanindra/rails/activerecord/lib/active_record/transactions.rb:319:in `with_transaction_returning_status'
      /home/phanindra/rails/activerecord/lib/active_record/transactions.rb:269:in `block in save'
      /home/phanindra/rails/activerecord/lib/active_record/transactions.rb:280:in `rollback_active_record_state!'
      /home/phanindra/rails/activerecord/lib/active_record/transactions.rb:268:in `save'
      /home/phanindra/rails/activerecord/lib/active_record/persistence.rb:37:in `create'
      ./app/controllers/admin/items_controller.rb:11:in `create'
      /home/phanindra/rails/actionpack/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
      /home/phanindra/rails/actionpack/lib/abstract_controller/base.rb:189:in `process_action'
      /home/phanindra/rails/actionpack/lib/action_controller/metal/rendering.rb:10:in `process_action'
      /home/phanindra/rails/actionpack/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
      /home/phanindra/rails/activesupport/lib/active_support/callbacks.rb:393:in `_run__143383953690284082__process_action__callbacks'
      /home/phanindra/rails/activesupport/lib/active_support/callbacks.rb:78:in `run_callbacks'
      /home/phanindra/rails/actionpack/lib/abstract_controller/callbacks.rb:17:in `process_action'
      /home/phanindra/rails/actionpack/lib/action_controller/metal/rescue.rb:29:in `process_action'
      /home/phanindra/rails/actionpack/lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
      /home/phanindra/rails/activesupport/lib/active_support/notifications.rb:158:in `block in instrument'
      /home/phanindra/rails/activesupport/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
      /home/phanindra/rails/activesupport/lib/active_support/notifications.rb:158:in `instrument'
      /home/phanindra/rails/actionpack/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
      /home/phanindra/rails/actionpack/lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
      /home/phanindra/rails/activerecord/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
      /home/phanindra/rails/actionpack/lib/abstract_controller/base.rb:136:in `process'
      /home/phanindra/rails/actionpack/lib/abstract_controller/rendering.rb:44:in `process'
      /home/phanindra/rails/actionpack/lib/action_controller/metal.rb:195:in `dispatch'
      /home/phanindra/rails/actionpack/lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
      /home/phanindra/rails/actionpack/lib/action_controller/metal.rb:231:in `block in action'
      /home/phanindra/rails/actionpack/lib/action_dispatch/routing/route_set.rb:77:in `call'
      /home/phanindra/rails/actionpack/lib/action_dispatch/routing/route_set.rb:77:in `dispatch'
      /home/phanindra/rails/actionpack/lib/action_dispatch/routing/route_set.rb:45:in `call'
      /home/phanindra/rails/actionpack/lib/action_dispatch/journey/router.rb:69:in `block in call'
      /home/phanindra/rails/actionpack/lib/action_dispatch/journey/router.rb:57:in `each'
      /home/phanindra/rails/actionpack/lib/action_dispatch/journey/router.rb:57:in `call'
      /home/phanindra/rails/actionpack/lib/action_dispatch/routing/route_set.rb:614:in `call'
      /home/phanindra/rails/actionpack/lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
      /home/phanindra/rails/actionpack/lib/action_dispatch/middleware/params_parser.rb:30:in `call'
      /home/phanindra/rails/actionpack/lib/action_dispatch/middleware/flash.rb:233:in `call'
      /home/phanindra/rails/actionpack/lib/action_dispatch/middleware/cookies.rb:443:in `call'
      /home/phanindra/rails/activerecord/lib/active_record/query_cache.rb:36:in `call'
      /home/phanindra/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:632:in `call'
      /home/phanindra/rails/actionpack/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
      /home/phanindra/rails/activesupport/lib/active_support/callbacks.rb:373:in `_run__2084705409073281596__call__callbacks'
      /home/phanindra/rails/activesupport/lib/active_support/callbacks.rb:78:in `run_callbacks'
      /home/phanindra/rails/actionpack/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
      /home/phanindra/rails/actionpack/lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
      /home/phanindra/rails/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb:18:in `call'
      /home/phanindra/rails/actionpack/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
      /home/phanindra/rails/railties/lib/rails/rack/logger.rb:32:in `call_app'
      /home/phanindra/rails/railties/lib/rails/rack/logger.rb:16:in `block in call'
      /home/phanindra/rails/activesupport/lib/active_support/tagged_logging.rb:67:in `block in tagged'
      /home/phanindra/rails/activesupport/lib/active_support/tagged_logging.rb:25:in `tagged'
      /home/phanindra/rails/activesupport/lib/active_support/tagged_logging.rb:67:in `tagged'
      /home/phanindra/rails/railties/lib/rails/rack/logger.rb:16:in `call'
      /home/phanindra/rails/actionpack/lib/action_dispatch/middleware/request_id.rb:21:in `call'
      /home/phanindra/rails/activesupport/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
      /home/phanindra/rails/actionpack/lib/action_dispatch/middleware/static.rb:63:in `call'
      /home/phanindra/rails/railties/lib/rails/engine.rb:508:in `call'
      /home/phanindra/rails/railties/lib/rails/application.rb:95:in `call'
      ./features/step_definitions/item_steps.rb:12:in `/^I press "([^\"]*)"$/'
      features/manage_items.feature:10:in `When I press "Create"'
    Then I should be on the admin items page # features/step_definitions/item_steps.rb:15
    And I should see "Shampoo"               # features/step_definitions/item_steps.rb:19

Failing Scenarios:
cucumber features/manage_items.feature:6 # Scenario: Items List

1 scenario (1 failed)
6 steps (1 failed, 2 skipped, 3 passed)
0m3.476s

看起来您正在使用 DatabaseCleaner,它默认在事务中运行您的每个功能。同时,ActiveRecord 本身在保存项目时启动一个事务。不幸的是,SQLite 不支持嵌套事务。

If you 改变你的DatabaseCleaner策略 https://github.com/bmabey/database_cleaner#how-to-use to :truncation它应该避免这个错误。或者,您可以针对功能更强大的数据库(例如 PostgreSQL)进行测试。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用cucumber测试时sql错误无法在事务内启动事务 的相关文章

  • 如何授予所有表的 REFERENCES 权限

    我必须授予REFERENCES登录权限说sql login 我可以给予资助REFERENCES对单个表的权限 例如 GRANT REFERENCES ON Mytable TO sql login 有什么办法可以授予REFERENCES允许
  • 始终等待页面加载到 PageObjects 上

    因此 当出现问题时 我只是创建了一个简单的 selenium JBehave 代码 我将首先发布简化的代码 然后稍后解释我的问题是什么 所以这里我们有一个简单的 AbstractClass 它将在我的 PageObjects 上继承 此类仅
  • 插入多行并返回主键时 Sqlalchemy 的奇怪行为

    插入多行并返回主键时 我注意到一些奇怪的事情 如果我在 isert 查询中添加使用参数值 我会得到预期的行为 但是当将值传递给游标时 不会返回任何内容 这可能是一个错误还是我误解了什么 我的sqlachemy版本是0 9 4 下面如何重现错
  • 如何在 JS Rails 响应中包含 HTML?

    我有一个响应 HTML 和 JS AJAX 查询的 FooController app controllers foo controller rb class FooController lt ApplicationController l
  • TOAD 将 &String 视为绑定变量

    我正在使用 Oracle Data Integrator 开发一些 ETL 有时会使用 TOAD 测试部分代码 今天我遇到了 TOAD 的问题 我有一行像 AND column value like DEV PROD 当我尝试运行包含上面过
  • sql查询将两列与一列连接起来

    我在 MS Access 2010 中有 2 个表 如下所示 USERS u id u name LOAN l id l from ref users u id l to ref users u id l amount Users u id
  • SPARK SQL - 当时的情况

    我是 SPARK SQL 的新手 SPARK SQL 中是否有相当于 CASE WHEN CONDITION THEN 0 ELSE 1 END 的内容 select case when 1 1 then 1 else 0 end from
  • 根据表sql中的行替换字符串中的字符

    我需要用一些映射的字符替换字符串中的字符列表 我有一个表 dbo CharacterMappings 有 2 列 CharacterToFilter 和 ReplacementCharacter 假设这个表中有3条记录 Filter Rep
  • 将 SQL 数据中的一行映射到 Java 对象

    我有一个 Java 类 其实例字段 以及匹配的 setter 方法 与 SQL 数据库表的列名相匹配 我想优雅地从表中获取一行 到 ResultSet 中 并将其映射到此类的实例 例如 我有一个 Student 类 其中包含实例字段 FNA
  • SQLite HAVING 比较错误

    我有一个测试 SQLite 表 用于存储带有值的报告数据 CREATE TABLE IF NOT EXISTS test fact daily revenue date TEXT revenue NUMERIC product TEXT I
  • 如何创建与 ActiveResource 对象的 ActiveRecord 关系?

    假设我正在为一家已经拥有 People 应用程序的出版公司编写一个图书馆应用程序 所以在我的图书馆应用程序中我有 class Person lt ActiveResource Base self site http api people m
  • SQL Server 用分隔符分割字符串

    我有一个输入字符串 100 2 3 101 2 1 103 2 3 我想解析它并将其添加到具有 3 列的表中 因此它应该是 f x col1 col2 col3 100 2 3 类似的其他数据以逗号分隔作为记录和 作为列 Thanks ni
  • 复选框上的数据绑定

    我目前正在将数据从 SQL 数据库之一提取到我的应用程序中 我可以让它适用于我的文本框和其他项目 但是 我似乎无法让它适用于复选框 这是我正在使用的代码 DataTable dt new DataTable dt using SqlConn
  • 如何从rails控制台将数据添加到数据库

    我有一个User model gt gt u User new gt
  • Mysql 检索所有有限制的行

    我想检索特定用户的所有行 限制为 0 x 所以我只是想问是否有任何方法可以检索 mysql 中的所有行 而不调用返回 x 的 count id 的方法 而不重载现有函数 该函数在查询中根本没有限制 与我们的 string Relace 功能
  • SQL 删除表并重新创建并保留数据

    在我们最初的设计中 我们搞砸了表中的外键约束 现在表已充满数据 我们无法在不删除表中所有记录的情况下更改它 我能想到的唯一解决方案是创建一个备份表并将所有记录放在那里 然后删除所有记录 更改表并开始将它们添加回来 还有其他 更好 的想法吗
  • 使用 JSON 参数的 Postgres 批量 INSERT 函数

    这是一个plpgsqlpostgres 的函数9 6 它试图INSERT一行 如果插入没有失败 由于违反键约束 那么它会运行更多命令 CREATE FUNCTION foo int text text RETURNS void AS BEG
  • 回形针不支持 .doc 文件

    在 Rails 4 0 2 中 我使用回形针 gem 上传文件 但它不支持 doc 文件 在文件上传字段下方 显示一条错误消息 扩展名与其内容不匹配 在模型中 检查内容类型的验证如下 validates attachment content
  • 列中差异的数量

    我想检索一列每行中有多少个字母的差异 例如 如果您有一个值 test 而另一行有一个值 testing 则 test 和 testing 之间的差异为 4 个字母 该列的数据值为 4 I have reflected about it an
  • Rails 3.1+ 的 Jasmine 与 Mocha JavaScript 测试 [已关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我对茉莉花有经验并且非常喜欢它 有谁有 Jasmine 和 Mocha 的经验 特别是 Rails 的经验吗 我想知道是否值得转用 我已经在 J

随机推荐