测试时错误的 URI(不是 URI?) |规格 [英] bad URI(is not URI?) while testing | Rspec

查看:50
本文介绍了测试时错误的 URI(不是 URI?) |规格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

测试结果如下:

1) 管理员可以编辑酒店失败/错误:访问 edit_admin_hotel_path(hotel)URI::InvalidURIError:错误的 URI(不是 URI?):# ./spec/requests/admin_spec.rb:32:in ‘block (2 levels) in <top (required)>’2) 管理员可以编辑用户失败/错误:访问 edit_admin_user_path(admin)URI::InvalidURIError:错误的 URI(不是 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#editedit_admin_user GET/admin/users/:id/edit(.:format) admin/users#edit

如果我启动服务器并手动检查一切正常.所以我不知道这些问题从何而来.谢谢你的帮助!还有我的 admin_spec.rb 文件:

需要'spec_helper'描述管理员"做let(:admin) { FactoryGirl.create(:user) }let(:hotel) { FactoryGirl.create(:hotel) }之前(:每个)做sign_up_as_admin 管理员访问 admin_hotels_path结尾主题{页面}它 { 期望(页面).拥有_内容(管理酒店")}它 { 期望(页面).to have_content("管理用户") }它 { 期望(页面).to have_link("退出") }它 { 期望(页面).to have_content("酒店列表") }它 { 期望(页面).拥有_内容(你好,管理员")}它可以添加酒店"吗click_link "添加酒店"期望(current_path).to eq(new_admin_hotel_path)fill_in 'name', with: "TestHotel"填写'价格',用:666"fill_in 'star_rating', with: "5"期望 { click_button "Submit" }.to change(Hotel,:count).by(1)期望(current_path).to eq(admin_hotel_path(1))结尾它可以编辑酒店"做访问 edit_admin_hotel_path(hotel)结尾它可以删除酒店"吗访问 admin_hotel_path(酒店)期望 { click_link "Delete hotel" }.to change(Hotel,:count).by(-1)#expect { click_link "Delete hotel" }.to redirect_to(admin_hotels_path)结尾它可以创建一个新用户"做click_link "添加用户"期望(current_path).to eq(new_admin_user_path)expect(page).to have_content("创建新用户")fill_in "姓名", :with =>用户"填写电子邮件",:with =>user@auser.com"填写密码",:with =>用户密码"fill_in "password_confirmation", :with =>用户密码"期望 { click_button "Create User" }.to change(User,:count).by(1)期望(current_path).to eq(admin_users_path)结尾它可以编辑用户"做访问 edit_admin_user_path(admin)结尾结尾

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

# GET admin/users/1/edit定义编辑@user = User.find(params[:id])呈现编辑",状态:302结尾# PATCH/PUT admin/users/1定义更新@user = User.find(params[:id])如果@user.try(:update_attributes, user_params)渲染编辑",注意:用户已成功更新."别的渲染动作:'编辑'结尾结尾私人的定义用户参数params.require(:user).permit(:name, :email, :password,:password_confirmation, :admin)结尾

还有 user/edit.html.erb:

<% provide(:title, "编辑用户") %><h1>更新配置文件</h1><div class="row"><div class="span6 offset3"><%= form_for([:admin, @user]) 做 |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, "确认密码" %><%= f.password_field :password_confirmation %><%= f.submit "保存更改" %><%结束%><%= button_to '删除用户', [:admin, @user], :data =>{ 确认:'你确定吗?}, 方法: :delete %>

更新 1:我发现在测试编辑操作时,hotel_controller 和 comment_controller 中也存在这些 错误 URI(不是 URI?) 错误.我所有控制器中的编辑操作中的这些错误,我不知道它们是什么 :(

解决方案

您的编辑操作错误:

def 编辑@user = User.find(params[:id])呈现编辑",状态:302结尾

302 状态意味着响应是重定向,并且响应中的 Location 标头包含要重定向到的 URI(相对或绝对).Capybara 将寻找这个标头,并且可能会尝试执行 URI.parse(nil).

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

I`ve got these strange issues I dont know how to fix it. Please help me if you can.

Here is a testing result:

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 shows me nice edit routes for users and hotels:

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

And everything works just fine if I start server and check it manually. So I have no idea where these issues comes from. Thanks for any help! And my admin_spec.rb file:

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

Edit/update actions in 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

And user/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>

Update 1: I found out that these bad URI(is not URI?) errors also in hotel_controller and comment_controller while testing edit action. These errors in all of my controllers in edit actions and I dont know what cousing them :(

解决方案

Your edit action is wrong:

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

A 302 status means that the response is a redirect and that the Location header in the response contains the URI (relative or absolute) to redirect to. Capybara will be looking for this header and probably winds up trying to do URI.parse(nil).

It's not clear to me why you are setting the status here at all

这篇关于测试时错误的 URI(不是 URI?) |规格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆