数据库清理程序不清除一个黄瓜方案

时间:2013-08-21 15:24:46

标签: ruby-on-rails-3 testing cucumber database-cleaner

所以我的团队正在使用Cucumber,Rspec和使用这些测试框架所需的所有宝石驱逐我们的rails应用程序(调查和精简电子记录系统)。我正在设置Jenkins CI服务器,并希望在我们的测试,开发和登台环境中标准化我们的数据库(我们最终选择了MySQL)。

在将测试环境从SQlite切换到MySQL后,我们发现了一些与缓存相关的测试错误,我们通过使用相对ID而不是硬编码来解决这些错误。例如:

describe "#instance_method" do
  before(:each) do
  @survey = FactoryGirl.create(:survey)
  @question_1 = FactoryGirl.create(:question)
  @question_2 = FactoryGirl.create(:question)
  ....
  end
  context "question has no requirements" do
    it "should match" do
      # below breaks under MySQL (and postgres), but not SQlite
      expect { @survey.present_question?(2) }.to be_true
      # always works
      expect { @survey.present_question?(@question_2.id) }.to be_true
    end
  end
end

在解决了这个失败的规范后,我解决了一些无关的ajax问题,这些问题一直困扰着我们的测试套件。我以为我最终掌握了测试艺术,我自信地跑了rake。我遇到了这个不友好的景象:

screen shot 2013-08-20 at 8 12 42 pm

当然,cucumber features/presenting_default_questions.feature当孤立地奔跑时会下雨。

失败的情景:

@javascript
Feature: Dynamic Presentation of Questions
  In order to only answer questions that are relevant considering previously answered      questions
  As a patient
  I want to not be presented questions that illogical given my previously answered question on the survey

  Scenario: Answering a question that does not depend on any other questions
    Given I have the following questions:
      | prompt             | datatype | options | parent_id | requirement |
      | Do you like cars?  | bool     |         |           |             |
      | Do you like fruit? | bool     |         |           |             |
    When I visit the patient sign in page
    And I fill out the form with the name "Jim Dog", date of birth "1978-03-30", and gender "male"
    And I accept the waiver
    Then I should see the question "Do you like cars"
    When I respond to the boolean question with "Yes"
    Then I should see the question "Do you like fruit?"
    When I respond to the boolean question with "No"
    Given I wait for the ajax request to finish
    Then I should be on the results page

相关步骤:

Then(/^I should be on the results page$/) do
  # fails under ``rake`` passes when run in isolation
  current_path.should == results_survey_path(1)
end

Then(/^I should be on the results page$/) do
  # passes in isolation and under ``rake``
  current_path.should == results_survey_path(Survey.last.id)
end

除了使用Capybara.javascript_driver = :webkit之外,黄瓜/数据库清理器配置未从rails g cucumber:install修改。

似乎失败的rspec和黄瓜测试都遭受同样的索引问题。虽然上面提出的解决方案有效,但是它非常笨拙并且提出了一个问题,即为什么简单的绝对索引不起作用(在每个场景和特征之间清除所有数据库之后)。我的测试有什么问题吗?使用database_cleaner?

如果有更多代码可以提供帮助,请与我们联系!

Relavent gem版本:

  • activemodel(3.2.14)
  • 黄瓜(1.3.6)
  • cucumber-rails(1.3.1)
  • database_cleaner(1.0.1)
  • capybara(2.1.0)
  • capybara-webkit(1.0.0)
  • mysql2(0.3.13)
  • rspec(2.13.0)

1 个答案:

答案 0 :(得分:1)

问题似乎是您错过了database_cleaner方案的Cucumber代码。

您提到database_cleaner方案的RSpec代码,但您似乎错过了Cucumber env的类似内容。 RB:

begin
  require 'database_cleaner'
  require 'database_cleaner/cucumber'

  DatabaseCleaner.strategy = :truncation
rescue NameError
  raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end

Around do |scenario, block|
  DatabaseCleaner.cleaning(&block)
end

你有这个代码吗?