Rspec/Capybara 功能测试未 运行,即使 _spec.rb 结束

Rspec/Capybara features tests not running, even with _spec.rb ending

当我 运行 我的 Rspec 特征示例时,它们没有被检测到。当我指定到我的 /features 文件夹的路径时,我收到的消息是“找不到示例”。我不确定这是需求问题还是我的测试遗漏了什么。

我的功能测试:

require "rails_helper"
require 'capybara/rspec'



feature "user creates student", :type => :feature do
background do
    user = create :user
scenario "with valid data" do
    visit '/students/new'
    within("form") do
        fill_in ":first_name", :with => 'jason'
        fill_in ":last_name", :with => 'foobar'
        fill_in ":user_id", :with => '1'
    end
    click_button 'Submit'
    expect(page).to have_content 'jason foobar'

  end
end


  feature "user cannot create student" do
   background do
    user = create :user
    scenario "with invalid data" do
    visit '/students/new'
    within("form") do
        fill_in ":first_name", :with => :nil
        fill_in ":last_name", :with => 'foobar'
        fill_in ":user_id", :with => '1'
    end
    click_button 'Submit'
    expect(studnt.errors[:first_name]).to include("can't be blank")

  end
end
 end


 end

您的规范正在 运行,但由于格式不正确,其中没有示例。

如果您正确地格式化它(例如在缩进方面),它会更清楚,但是您的 background 块包括两个相关的场景。

您需要删除文件中的最后两个 end 语句,并在每个 background 的末尾插入 end 语句,如下所示:

require "rails_helper"
require 'capybara/rspec'

feature "user creates student", :type => :feature do
  background do
    user = create :user
  end

  scenario "with valid data" do
    visit '/students/new'
    within("form") do
      fill_in ":first_name", :with => 'jason'
      fill_in ":last_name", :with => 'foobar'
      fill_in ":user_id", :with => '1'
    end
    click_button 'Submit'
    expect(page).to have_content 'jason foobar'
  end
end

feature "user cannot create student" do
  background do
    user = create :user
  end

  scenario "with invalid data" do
    visit '/students/new'
    within("form") do
      fill_in ":first_name", :with => :nil
      fill_in ":last_name", :with => 'foobar'
      fill_in ":user_id", :with => '1'
    end
    click_button 'Submit'
    expect(studnt.errors[:first_name]).to include("can't be blank")
  end
end