Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 水豚不运行自定义类(PORO)_Ruby_Ruby On Rails 4_Selenium_Capybara_Factory Bot - Fatal编程技术网

Ruby 水豚不运行自定义类(PORO)

Ruby 水豚不运行自定义类(PORO),ruby,ruby-on-rails-4,selenium,capybara,factory-bot,Ruby,Ruby On Rails 4,Selenium,Capybara,Factory Bot,对于我的一个表单,我创建了一个普通的ruby对象来为select表单提供数据 class SearchFields def types Card.select(:types).order('types ASC').uniq.collect.each do |c| if c.types.nil? next else types = c.types.delete("[:&,\"]").split().join(" ")

对于我的一个表单,我创建了一个普通的ruby对象来为select表单提供数据

class SearchFields
  def types
    Card.select(:types).order('types ASC').uniq.collect.each do |c|
      if c.types.nil?
        next
      else
        types = c.types.delete("[:&,\"]").split().join(" ")
        [types, c.types]
      end
    end
  end
end
在我的控制器中,我调用如下方法:

def index
  @card_types = SearchFields.new.types
end
在我看来(这是部分原因):

我正在使用Selenium运行Capybara来运行测试,但是当呈现部分时,select表单不包含任何数据。
Card
对象是使用FactoryGirl创建的,我知道它已成功创建,因为前面的测试是有效的

似乎正在发生的事情是没有创建SearchFields对象。如果我在控制台中键入@card\u types,它将返回
nil
。 有人知道这里发生了什么,为什么没有创建对象吗?任何信息都将不胜感激!:)

更新: 以下是数据库_cleaner.rb文件:

RSpec.configure do |config|

  config.before(:suite) do
    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

end

您确定工厂正在卡的“类型”属性中创建某些内容吗?这也是您的第一个js:true测试,还是您的其他js测试工作正常?请确保舒尔不会在数据库事务中运行您的测试。在这种情况下,在测试web服务器的线程中看不到数据库中的任何更改(表中的任何新记录)。@TomWalpole类型属性已成功创建。我还用
pry
对它进行了测试,以确保安全。这是我的第二个
js:true
测试。在此之前的测试测试了“基本搜索”功能,并成功完成。正如@andrykonchin所评论的,请确保已正确设置数据库清理器,以使用截断而不是基于事务的清理,并且已禁用事务性固定装置。如果您使用的是基于事务的测试,那么在测试线程中使用pry进行测试实际上并没有向您显示应用程序所看到的内容,因为我已经在文章中添加了数据库\u cleaner配置。据我所知,由于它运行的是
js:true
,所以它使用的是
:截断
。在
js:
未设置为
true
的情况下运行
:transaction
是否重要?
feature "User uses Advanced Card Search", js: true do
  let!(:card) { create(:card) }

  background do
    visit cards_path
    expect(page).to have_content("Search for cards you would like")
    click_on "Advanced Search"
    fill_in("cards_autocomplete_name", with: card.name)
    select(card.cmc, from: "q_cmc_gteq")
    select(card.cmc, from: "q_cmc_lteq")
    select(card.colors, from: "q_colors_cont")
    select(card.types, from: "q_types_cont") # here is where it fails
    fill_in("q_printings_cont", with: card.printings)
    select(card.legalities, from: "q_legalities_cont")
    click_on "card-search-submit"

    expect(page).to have_css("#cards-table td#card-name")
  end

  scenario "and successfully finds card" do
    expect(page.first('#cards-table td#card-name').text).to eq(card.name)
  end
end
RSpec.configure do |config|

  config.before(:suite) do
    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

end