Capybara抛出错误,但浏览器确实没问题

时间:2015-09-18 13:47:06

标签: ruby ruby-on-rails-4 rspec capybara

这个测试:

describe "Regions Management", type: :feature do

  feature "Editing a Region" do
    scenario "with valid parameters" do
      region = create(:region)
      visit edit_region_path(region)

      within "#edit_region_#{region.id}" do
        fill_in 'region_name', with: "Edited Region"
        click_button I18n.t('helpers.submit.update', model: Region.model_name.human)
        expect(current_path).to eql(region_path(region))
      end
    end
  end

end

失败并出现以下错误:

ActionView::Template::Error:
   No route matches {:action=>"show", :controller=>"regions", :id=>nil} missing required keys: [:id]

region对象确实有一个id:

[27, 36] in /home/leandro/projects/optimaster3/spec/features/regions_management_spec.rb
   27:       region = create(:region)
   28:       visit edit_region_path(region)
   29:       within "#edit_region_#{region.id}" do
   30:         fill_in 'region_name', with: "Edited Region"
   31:         byebug
=> 32:         click_button I18n.t('helpers.submit.update', model: Region.model_name.human)
   33: 
   34:         expect(current_path).to eql(region_path(region))
   35:       end
   36:     end
(byebug) region.reload
#<Region:0x000000099bba28>
(byebug) region.inspect
#<Region id: 27, name: "Region 26", ancestry: nil, notes: "Some notes", created_at: "2015-09-18 14:42:40", updated_at: "2015-09-18 14:42:40", parent_id: nil, lft: 1, rgt: 2, children_count: nil>
(byebug) click_button I18n.t('helpers.submit.update', model: Region.model_name.human)
*** ActionView::Template::Error Exception: No route matches {:action=>"show", :controller=>"regions", :id=>nil} missing required keys: [:id]

这是我的routes.rb:

resources :regions do
  collection do
    get 'regions_list'
  end
end

我可以在浏览器上更新某个区域,没问题。但是在运行测试时会抛出此错误。

这是创建测试,并传递OK:

  feature "Creating a Region" do
    scenario "with valid parameters" do
      region = region_attributes = attributes_for(:region)
      visit new_region_path

      within "#new_region" do
        fill_in 'region_name', with: region_attributes[:name]
        fill_in 'region_notes', with: region_attributes[:notes]
        click_button I18n.t('helpers.submit.create', model: Region.model_name.human)
        expect(current_path).to eql(region_path)
      end
    end
  end

end

0 个答案:

没有答案