水豚的 ID 路径

Id path on capybara

我有问题。

我正在尝试使用 capybara 进行集成测试,但是当我测试上下文 "edit new tarefa" 时,我无法在我的页面上获取 id 以便它进行访问。我正在使用设计,所以我在代码的开头创建了用户。

代码如下:

require 'rails_helper'

describe "Tarefas", :type => :feature do
  feature "New Tarefa" do
    background do
      user = FactoryGirl.create(:user)
      login_as(user, :scope => :user)
    end

    context "create new tarefa" do
      it "preenchendo os campos" do
        visit '/tarefas/new'
        within("#new_tarefa") do
          fill_in 'tarefa_titulo', with: 'user@example.com'
          fill_in 'tarefa_descricao', with: 'password'
          fill_in 'tarefa_data', with: '18/06/1990 20:00'
        end
        click_button 'submit'
        expect(page).to have_content 'Mostra a tarefa selecionada'
      end
    end

    context "edit new tarefa" do
      it "alterando os campos" do
        visit "tarefas/#{Tarefa.last.id}/edit"
        within("#new_tarefa") do
          fill_in 'tarefa_titulo', with: 'user@exa12mple.com'
          fill_in 'tarefa_descricao', with: 'passw213ord'
          fill_in 'tarefa_data', with: '18/06/1990 21:00'
        end
        click_button 'submit'
        expect(page).to have_content 'Mostra a tarefa selecionada'
      end
    end
  end
end

您不应依赖在 "create new tarefa" 中创建的 tarefa 在您的下一个规范中可用:

  • 由于顺序是随机的,因此无法使用。
  • 它会创建一个耦合,其中一个规范依赖于另一个规范的结果。

相反,您想在每个规范后使用 database_cleaner 清理数据库,并使用 letlet! 设置每个规范的要求:

require 'rails_helper'

# You can use `feature` as a top level block. 
# No need to nest it in descibe.
RSpec.feature "New Tarefa" do

  let(:user) { FactoryGirl.create(:user) }
  let(:tarefa) { FactoryGirl.create(:tarefa) }

  background do
    login_as(user, scope: :user)
  end

  context "create new tarefa" do
    it "preenchendo os campos" do
      visit '/tarefas/new'
      within("#new_tarefa") do
        fill_in 'tarefa_titulo', with: 'user@example.com'
        fill_in 'tarefa_descricao', with: 'password'
        fill_in 'tarefa_data', with: '18/06/1990 20:00'
      end
      click_button 'submit'
      expect(page).to have_content 'Mostra a tarefa selecionada'
    end
  end

  context "edit new tarefa" do
    it "alterando os campos" do
      # tarefa is created when you reference it.
      visit "tarefas/#{tarefa.to_param}/edit"
      within("#new_tarefa") do
        fill_in 'tarefa_titulo', with: 'user@exa12mple.com'
        fill_in 'tarefa_descricao', with: 'passw213ord'
        fill_in 'tarefa_data', with: '18/06/1990 21:00'
      end
      click_button 'submit'
      expect(page).to have_content 'Mostra a tarefa selecionada'
    end
  end
end