ActiveRelation Query在Rails System Test中为空,但可以交互工作

时间:2019-05-06 18:01:14

标签: ruby-on-rails

在交互式会话中有效,但在Rails系统测试中失败:

使用byebug,该脚本在显示视图中意外返回空

@item.children.where(done: false)

如果我这样跳过ActiveRelation,我会正确看到该对象

@item.children

#<ActiveRecord::Associations::CollectionProxy [#<Item id: 980190964, title: "my item", done: false

此记录意外返回1条记录

@item.children.where.not(done:false)

设置(效果很好)

  setup do
    @user = users(:one)
    visit login_url
    fill_in "Email", with: @user.email
    fill_in "Password", with: 'secret'
    click_on "Login"    
  end

测试(效果很好,但项目不会显示!)

  test "create one entry" do
    visit item_url(@user.top_item)
    fill_in('item[title]', with: 'my item')
    find_field('item[title]').native.send_key(:enter)    
    visit current_path
    assert_selector "h5", text: "parentObject", match: :one
  end

表演动作的一部分:

@children = @item.children
@newItem = current_user.items.new

部分展示视图:

<% @children.where(done: false).each do |item| %>

即使@children属性为空。重新加载没有任何区别。

1 个答案:

答案 0 :(得分:1)

当我将以下设置设置为false时起作用。我直接在sqlite3中查看了架构,“ done”字段的默认值为“ false”而不是“ 0”。但是,为什么那么顺利地进行开发而不进行测试呢?可能与仍使用“ false”的yaml文件存在一些冲突。因此,我会坚持使用不推荐使用的设置一段时间,直到我完全理解其中的含义,以后再进行适当的迁移。

Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = false

我追求真实的理由:

不建议使用警告:离开ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer 设置为false弃用。 SQLite数据库已使用“ t”和“ f”进行序列化 布尔值,并且必须将旧数据转换为1和0(其本机布尔值 序列化),然后将此标志设置为true。转换可以完成 通过设置运行的rake任务

ExampleModel.where("boolean_column = 't'").update_all(boolean_column: 1)
ExampleModel.where("boolean_column = 'f'").update_all(boolean_column: 0)

对于所有模型和所有布尔列,此后必须将标志设置为 将以下内容添加到application.rb文件中,则为true:

Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true
相关问题