【问题标题】:Rails tutorial - cannot get patch test to work - patch unrecognized methodRails 教程 - 无法让补丁测试工作 - 补丁无法识别的方法
【发布时间】:2013-07-23 03:24:08
【问题描述】:

这是一些 sn-ps 代码 - 如果您需要更多,请告诉我,我会发布它。

class UsersController < ApplicationController

  before_filter :signed_in_user, only: [:edit, :update]
  before_filter :correct_user,   only: [:edit, :update]

  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
        sign_in @user
        flash[:success] = "Welcome to the Sample App!"
        redirect_to @user
    else
        render 'new'
    end
  end

  def edit
  end

  def update
    if @user.update_attributes(params[:user]) # user_params
        flash[:success] = "Profile updated"
        sign_in @user
        redirect_to @user
    else
        render 'edit'
    end
  end

  private

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

    # Before filters

    def signed_in_user
        redirect_to signin_url, notice: "Please sign in." unless signed_in?
    end

    def correct_user
        @user = User.find(params[:id])
        redirect_to(root_path) unless current_user?(@user)
    end
end

然后是单元测试...

describe "authorization" do
...
                describe "submitting a PATCH request to the Users#update action" do
                    before { patch user_path(wrong_user) }
                    specify { expect(response).to redirect_to(root_path) }
                end

如果我运行它,我会收到消息:

FailureError: 在 { 补丁 user_path(wrong_user) } 之前 无方法错误: # RSpec::Core::ExampleGroup::Nested_3::Nested_1::Nested_3::Nested_2::Nested_2:0x 的未定义方法 `patch'...

如果我将补丁更改为发布,我会得到这个...

FailureError: 在 { post user_path(wrong_user) } 之前 动作控制器::路由错误: 没有路由匹配 [POST] "users/1497"


put 的错误实际上表明测试被重定向到登录路径,但是当我在浏览器中测试它时,它按预期重定向到了根路径。我想知道我的会话是否在测试中没有持续。我记得在以前的测试中让我遇到过这样的麻烦......

下面是剩下的测试代码:

    describe "for wrong user" do
        let(:user) { FactoryGirl.create(:user) }
        let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
        before { sign_in user, no_capybara: true }

        describe "visiting Users#edit page" do
            before { visit edit_user_path(wrong_user) }
            it { should_not have_title(full_title('Edit user')) }
        end

        describe "submitting a PATCH request to the Users#update action" do
            before { put user_path(wrong_user) }
            specify { expect(response).to redirect_to(root_path) }
        end
    end # end for wrong user

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.2 railstutorial.org


    【解决方案1】:

    patch 存在于 Rails 4 中,您使用的是 Rails 3.2(这就是您的问题标签中提到的),您需要改用 put

    【讨论】:

    • 看来我的问题是不知何故,我的用户没有登录。当我在浏览器中测试时,一切正常,但是当我运行单元测试时,put 实际上将我重定向到登录页面。但是,我之前的测试依赖于有效的 sign_in 方法......
    • 啊,好吧,我以为您使用的是 rails 3.2,下次请正确标记您的问题 :) 很高兴您找到了修复程序。干杯
    • 嗯,你是正确的 - put 工作,这揭示了真正的错误,哈哈,这是当提交一个 put 请求以更新错误的用户时,我被重定向到错误的页面。
    • 啊好! :) 所以我的回答是正确的,如果你不介意,请接受它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 2012-08-03
    • 2018-12-12
    相关资源
    最近更新 更多