使用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错误无法在事务内启动事务 的相关文章

  • 在实体框架 6 中使用 SqlQuery>

    我正在尝试在 EF 6 中执行 SQL 查询 select查询返回两个字符串列 例如select a b 并且可以有任意数量的行 我想将结果映射到字典 但我无法摆脱以下错误 错误 1 无法将类型 System Data Entity Inf
  • 无法访问 Big Query 中类型为 ARRAY> 的字段

    我正在尝试在 BigQuery 上使用标准 SQL 方言 即不是旧版 SQL 运行查询 我的查询是 SELECT date hits referer FROM refresh ga sessions xxxxxx LIMIT 1000 但不
  • Ruby on Rails:有关 validates_presence_of 的问题

    我的基于 ActiveRecord 的模型中有一个关系 如下所示 belongs to foo 我的模型应该始终在其中定义 foo 才能有效 我的问题是 当使用 validates presence 时 使用哪一个是合适的 validate
  • MySQL 5:我的 GROUP BY 字段的顺序重要吗?

    Peeps 我的 MySQL 查询中有一些聚合 计算字段 我的 GROUP BY 子句是动态生成的 具体取决于用户在 Web 表单中选择的选项 很好奇 GROUP BY 子句中列出的字段顺序是否会对计算产生任何影响 例如 SUM AVERA
  • SQL Server 中全文搜索的奇怪行为

    我的 MyTable 带有列消息 NVARCHAR MAX ID 为 1 的记录包含消息 0123456789333444 Test 当我运行以下查询时 DECLARE Keyword NVARCHAR 100 SET Keyword 01
  • 什么是动态 SQL 查询?何时需要使用动态 SQL 查询?

    什么是动态 SQL 查询 何时需要使用动态 SQL 查询 我正在使用 SQL Server 2005 这里有几篇文章 动态SQL简介 http www sqlteam com article introduction to dynamic
  • MySQL Tinybit(1) 通过视图的列

    我有一个连接 2 个表的视图 其中一个表具有表示布尔值的tinyint 1 类型的列 该表在连接时并不总是有条目 因此当行丢失时视图需要采用 0 false 值 我希望视图公开 TINYINT 1 类型且 NOT NULL 类型的列 因为它
  • Rails3 has_many 关系中子项计数的范围

    尝试在rails3中做一个范围 book has many chapters 我想要 range long 返回超过 10 章的书籍 如何最好地构建这个范围 不使用计数器缓存 thanks 这应该会让你继续 class Book scope
  • SQL Server 2008 GUID 列全为 0

    我希望这是我做的一个简单的傻事 我的数据库中有一个表 设置如下 column name widget guid data type uniqueidentifier allow nulls false default value newid
  • Ruby 2 升级破坏了 Nokogiri 和/或 open-uri 编码?

    将 Rails3 2 Ruby 1 9 应用程序升级到 Rails3 2 Ruby 2 1 2 时 我有一个谜团需要解决 Nokogiri 似乎崩溃了 因为它使用 open uri 改变了它的行为 没有改变 gem 版本 只是改变 ruby
  • 如何获得组中“中间”值的平均值?

    我有一个包含值和组 ID 的表 简化示例 我需要获取中间 3 个值的每组的平均值 因此 如果有 1 2 或 3 个值 则它只是平均值 但如果有 4 个值 它将排除最高值 5 个值将排除最高值和最低值 等等 我正在考虑某种窗口函数 但我不确定
  • Rails 中的漂亮路径

    我有一个类别模型 我使用默认的脚手架来路由它resources categories 我想知道是否有办法改变路径 category id to category name 我补充道 match categories name gt cate
  • 更高效的 LINQ 查询

    有人可以帮我将此查询循环变成高效的 Linq 查询吗 我将其加载到 TreeView 中 因此必须附加每个项目 包含也非常低效 延迟加载项目也不起作用 事实上 这个查询访问数据库的次数比应有的要多 public IQueryable
  • 将数据表传递到存储过程。有没有更好的办法?

    数据表可以以某种方式传递到 SQL Server 2005 或 2008 中吗 我知道标准方法似乎是将 XML 传递给 SP 并且可以通过某种方式轻松地将数据表转换为 XML 来实现这一点 将 NET 对象传递到 SP 怎么样 那可能吗 我
  • 合并两个ActiveRecord数组并按created_at排序

    books Book find all articles Articles find all 通过阅读来自http guides rubyonrails org layouts and rendering html http guides
  • 什么是更好的?子查询或内连接十个表?

    一个旧系统已抵达我们的办公室进行一些更改和修复 但它也存在性能问题 我们并不确切知道这种缓慢的根源是什么 当我们重构旧代码时 我们发现了几个具有以下模式的 sql 查询 出于示例目的 简化了查询 SELECT SELECT X FROM A
  • Rails has_many 到 has_many 具有多个模型

    模拟以下情况的最佳方法是什么 Word belongs to wordable polymorphic gt true Phrase has many words as gt workable belongs to story Line h
  • 获取特定时区一天开始时的时间对象

    如何获取代表给定时区特定日期的一天开始时间的 ruby Time 对象 date Date today date to time in time zone America New York beginning of day 目前输出 gt
  • 如何在sql server 2008R2中将单个单元格拆分为多个列?

    我想将每个名称拆分为各个列 create table split test value integer Allnames varchar 40 insert into split test values 1 Vinoth Kumar Raj
  • parent_id 是外键(自引用)并且为 null?

    浏览 Bill Karwin 的书 SQL Antipatterns 第 3 章 Naive Trees 邻接表 父子关系 有一个注释表的示例 CREATE TABLE Comments comment id SERIAL PRIMARY

随机推荐