1

所以我的团队正在使用 Cucumber、Rspec 和使用这些测试框架所需的所有 gem 来推出我们的 rails 应用程序(一个调查和 lite 电子记录系统)。我正在设置 Jenkins CI 服务器,并希望在我们的测试、开发和暂存环境中标准化我们的数据库(我们最终选择了 MySQL)。

在将测试环境从 SQlite 切换到 MySQL 后,我们发现了一些与缓存相关的测试错误,我们通过使用相对 id 而不是硬编码的 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。我遇到了这种不友好的景象:

屏幕截图 2013-08-20 在 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 和 cucumber 测试都遇到了同样的索引问题。虽然上面提出的解决方案有效,但它非常笨拙,并提出了一个问题,即为什么简单的绝对索引不起作用(在每个场景和功能之间清理了所有数据库之后)。我的测试有问题吗?使用 database_cleaner?

如果更多代码会有所帮助,请告诉我!

相关宝石版本:

  • 活动模型(3.2.14)
  • 黄瓜 (1.3.6)
  • 黄瓜导轨 (1.3.1)
  • database_cleaner (1.0.1)
  • 水豚 (2.1.0)
  • 水豚 webkit (1.0.0)
  • mysql2 (0.3.13)
  • rspec (2.13.0)
4

1 回答 1

1

问题似乎是您缺少场景的database_cleaner代码。Cucumber

您提到您拥有database_cleaner适用于您的场景的代码RSpec,但您似乎缺少Cucumberenv.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

你里面有这个代码吗?

于 2014-12-10T14:53:50.193 回答