即使_spec.rb结束,Rspec / Capybara的测试也没有运行

时间:2015-03-04 21:48:54

标签: rspec capybara rspec-rails

当我运行我的Rspec功能示例时,它们未被检测到。当我指定到my / 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

1 个答案:

答案 0 :(得分:1)

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

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

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

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