Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 水豚抛出错误,但浏览器没有问题_Ruby_Ruby On Rails 4_Rspec_Capybara - Fatal编程技术网

Ruby 水豚抛出错误,但浏览器没有问题

Ruby 水豚抛出错误,但浏览器没有问题,ruby,ruby-on-rails-4,rspec,capybara,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 fi

此测试:

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]
区域对象没有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]
我可以在浏览器上更新一个区域,没问题。但是在运行测试时,会抛出此错误

这是创建测试,通过了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

region=create(:region)
put region.inspect的输出是什么?看起来它没有ID。它有一个ID,已经通过eBug检查过了。已将其添加到答案中。是否尝试手动编辑区域?它也失败了吗?正如我所说:“我可以在浏览器上更新一个区域,没问题。”。因此,不,手动操作不会失败。您可以尝试在单击\按钮之前重新加载区域,然后检查其值吗?
  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