【问题标题】:bad URI(is not URI?) while testing | Rspec测试时错误的 URI(不是 URI?)|规格
【发布时间】:2015-05-30 04:44:59
【问题描述】:

我遇到了这些奇怪的问题,我不知道如何解决。如果可以,请你帮助我。

这是一个测试结果:

1) Admin can edit a hotel
     Failure/Error: visit edit_admin_hotel_path(hotel)
     URI::InvalidURIError:
       bad URI(is not URI?): 
     # ./spec/requests/admin_spec.rb:32:in `block (2 levels) in <top (required)>'

  2) Admin can edit a user
     Failure/Error: visit edit_admin_user_path(admin)
     URI::InvalidURIError:
       bad URI(is not URI?): 
     # ./spec/requests/admin_spec.rb:54:in `block (2 levels) in <top (required)>'

rake routes 向我展示了用户和酒店的漂亮编辑路线:

edit_admin_hotel  GET  /admin/hotels/:id/edit(.:format)   admin/hotels#edit
edit_admin_user   GET  /admin/users/:id/edit(.:format)     admin/users#edit

如果我启动服务器并手动检查它,一切都会正常工作。所以我不知道这些问题是从哪里来的。谢谢你的帮助! 还有我的 admin_spec.rb 文件:

require 'spec_helper'

describe "Admin"  do
    let(:admin) { FactoryGirl.create(:user) }
    let(:hotel) { FactoryGirl.create(:hotel) }


    before(:each) do
        sign_up_as_admin admin
        visit admin_hotels_path
    end

    subject { page }

    it { expect(page).to have_content("Manage Hotels") }
    it { expect(page).to have_content("Manage Users") }
    it { expect(page).to have_link("Sign out") }
    it { expect(page).to have_content("List of hotels") }
    it { expect(page).to have_content("Hello, Admin") }

    it "can add a hotel" do
        click_link "Add Hotel"
        expect(current_path).to eq(new_admin_hotel_path)
        fill_in 'name', with: "TestHotel"
        fill_in 'price', with: "666"
        fill_in 'star_rating', with: "5"
        expect { click_button "Submit" }.to change(Hotel,:count).by(1)
        expect(current_path).to eq(admin_hotel_path(1))
    end

    it "can edit a hotel" do
        visit edit_admin_hotel_path(hotel)
    end

    it "can delete a hotel" do
        visit admin_hotel_path(hotel)
        expect { click_link "Delete hotel" }.to change(Hotel,:count).by(-1)
        #expect { click_link "Delete hotel" }.to redirect_to(admin_hotels_path)
    end

    it "can create a new user" do
        click_link "Add User"
        expect(current_path).to eq(new_admin_user_path)
        expect(page).to have_content("Create New User")
        fill_in "Name",    :with => "user"
        fill_in "Email",    :with => "user@auser.com"
        fill_in "Password", :with => "user.password"
        fill_in "password_confirmation", :with => "user.password"
        expect { click_button "Create User" }.to change(User,:count).by(1)
        expect(current_path).to eq(admin_users_path)
    end

    it "can edit a user" do
        visit edit_admin_user_path(admin)
    end
end

在 users_controller.rb 中编辑/更新操作:

# GET admin/users/1/edit
  def edit
    @user = User.find(params[:id])
    render "edit", status: 302
  end

  # PATCH/PUT admin/users/1
  def update
    @user = User.find(params[:id])
    if @user.try(:update_attributes, user_params)
      render "edit", notice: 'User was successfully updated.'
    else
      render action: 'edit'
    end
  end

  private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation, :admin)
    end

还有用户/edit.html.erb:

<% provide(:title, "Edit user") %>
<h1>Update profile</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for([:admin, @user]) do |f| %>
      <%= render 'shared/error_messages', object: f.object %>

      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.label :password_confirmation, "Confirm Password" %>
      <%= f.password_field :password_confirmation %>

      <%= f.submit "Save changes" %>
    <% end %>
      <%= button_to 'Delete User', [:admin, @user], :data => {     confirm: 'Are you sure?' }, method: :delete %>
  </div>
</div>

更新 1: 我在测试编辑操作时发现这些bad URI(is not URI?) 错误也在hotel_controller 和comment_controller 中。我的所有控制器在编辑操作中都出现了这些错误,我不知道是什么导致了它们:(

【问题讨论】:

  • puts edit_admin_hotel_path(hotel).inspect 显示什么?
  • @Andy Waite "/admin/hotels/1/edit"
  • 如果您在将规范 edit_admin_hotel_path(hotel) 传递给 visit 助手之前打印,那么值是多少?
  • 如果在运行 rspec (-b) 时指定回溯,它是否指向规范以外的其他行?另外,当你打印出路径时,你是从你的规范中打印出来的吗?最后,您可以尝试打印edit_admin_hotel_url(hotel).inspect

标签: ruby-on-rails ruby rspec


【解决方案1】:

您的编辑操作错误:

def edit
  @user = User.find(params[:id])
  render "edit", status: 302
end

302 状态表示响应是重定向,并且响应中的 Location 标头包含要重定向到的 URI(相对或绝对)。 Capybara 将寻找此标头,并可能最终尝试执行 URI.parse(nil)

我不清楚你为什么要在这里设置状态

【讨论】:

    【解决方案2】:

    在您的 spec_helper 中是否包含 url_helpers?

    config.include Rails.application.routes.url_helpers
    

    【讨论】:

    • 感谢您的回答。我现在做了,但测试仍在下降。
    猜你喜欢
    • 2012-02-23
    • 2011-07-19
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    • 2018-04-08
    相关资源
    最近更新 更多