测试与"给出"

时间:2017-11-02 23:33:13

标签: capybara ruby-on-rails-5 rspec3

我有一个功能测试文件,单独运行时工作正常。但是,当我尝试运行所有测试时,同样的测试会给我一个错误。

我正在检查,我发现三个文件有相同的"给定"当我评论它们时,这个测试文件运行良好。

这是失败的规范(文件work_order_actions_spec.rb):

 feature "Work Order actions" do
  login_user

  given!(:client) { create(:client) }
  given!(:location) { create(:location, client: client) }
  given!(:contract) { create(:contract, client: client) }
  given!(:work_orders) { create_list :work_order, 3, client: client, contract: contract, location: location }
  given!(:other_work_orders) { create_list :work_order, 2 }

  ...

  context "open a new work_order" do
    given!(:responsible) { create(:user) }
    given!(:work_order) { attributes_for :work_order }

    scenario "with valid information should open successfully with :waiting_for_service status", js: true do
      visit work_orders_path
      all(".open_work_order").first.click
      expect(page).to_not have_css "#work_order_status"
      fill_in "work_order_opening_date", with: work_order[:opening_date]
      fill_in "work_order_service_description", with: work_order[:service_description]
      select_by_value work_order[:urgency], from: "work_order_urgency"
      select client.id, from: "work_order_client"
      select location.id, from: "work_order_location"
      select contract.id, from: "work_order_contract"
      select responsible.id, from: "work_order_responsible_id"
      find("input[type='submit']").click
      work_order = WorkOrder.last
      expect(page).to have_current_path work_order_path(work_order)
      expect(page).to have_content work_order.status_text
      expect(page).to have_content i18n_notice_responder(:create, WorkOrder)
    end

这是"给出"即存在于这三个文件之一(文件contract_actions_spec.rb):

feature "Contract actions" do
  login_user

  given(:client) { create(:client) }
  given!(:contracts) { create_list :contract, 5, client: client }
  given!(:other_contracts) { create_list :contract, 5 }
  ...
end

如果我评论这个"给予(:客户)"在我的文件contract_actions_spec.rb上,work_orders_spec.rb上的测试运行良好。如果我首先运行work_orders_spec.rb,这个测试也有效,一切运行良好。

这是一个错误视图: error

这是尝试选择字段的部分。这里有一个Ajax。选择客户端后,将使用Ajax填充字段合同。没有测试,没有其他冲突测试(作为contract_actions_spec.rb),它运行良好。

<div class="col-xl-4 col-lg-4 col-md-6 col-sm-12 col-xs-12">
  <div class="form-group select required work_order_client">
    <label class="control-label select required" for="work_order_client">
      <abbr title="required">*</abbr> Client
    </label>
    <select class="form-control select required" id="work_order_client" data-location-url="/clients/:client_id/locations" data-contract-url="/clients/:client_id/contracts" name="work_order[client_id]">
      <option value=""></option>
      <option value="892">Client 891</option>
      <option value="893">Client 892</option>
      <option value="894">Client 893</option>
      <option value="895">Client 894</option>
      <option value="896">Client 895</option>
      <option value="897">Client 896</option>
      <option value="898">Client 897</option>
    </select>
  </div>
</div>

<div class="col-xl-4 col-lg-4 col-md-6 col-sm-12 col-xs-12">
  <div class="form-group select required work_order_contract">
    <label class="control-label select required" for="work_order_contract">
      <abbr title="required">*</abbr> Contract
    </label>
    <select class="form-control select required" id="work_order_contract" name="work_order[contract_id]"></select>
  </div>
</div>

在这个项目中,我使用Docker和Selenium是一个容器,我配置Capybara使用这个远程Selenium

if ENV['SELENIUM_REMOTE_HOST']
  Capybara.javascript_driver = :selenium_remote_firefox
  Capybara.register_driver "selenium_remote_firefox".to_sym do |app|
    Capybara::Selenium::Driver.new(
      app,
      browser: :remote,
      url: "http://#{ENV['SELENIUM_REMOTE_HOST']}:4444/wd/hub",
      desired_capabilities: :firefox)
  end

  ip = `/sbin/ip route|awk '/scope/ { print $9 }'`
  ip = ip.gsub "\n", ""
  Capybara.server_port = "3000"
  Capybara.server_host = ip
  Capybara.app_host = "http://#{Capybara.server_host}:#{Capybara.server_port}"
end

在运行测试的容器上,ENV [&#39; SELENIUM_REMOTE_HOST&#39;]具有值&#39; selenium&#39; (我的容器与Selenium的名称o)

0 个答案:

没有答案