Rspec请求规范ActionController :: RoutingError

时间:2018-05-06 07:14:38

标签: ruby-on-rails rspec tdd rspec-rails

我试图在Rspec中重写我的测试,但我仍然试图对用户进行更新。 我确信这是一个我无法忽视的愚蠢的事情,但我到处寻找,无法找到答案。

这里是myspec文件:

require 'spec_helper'

describe UsersController, type: [:request, :controller]  do

before(:each) { host! 'localhost:3000/' }

describe "#update" do

it "doesn't update user" do
  form_params = {
    params: {
    id: 1,
    email: "hello@example",
    name: "hello"
    }
  }
  patch :update, params: form_params


    end
  end
end

似乎:更新部分是错误的,但就我在路线中看到的情况而言,没有其他方法可以调用它。

这是错误:

ActionController::RoutingError: No route matches [PATCH] "/update"

这是我的路线:

       new_user GET    /users/new(.:format)                    users#new
      edit_user GET    /users/:id/edit(.:format)               users#edit
           user GET    /users/:id(.:format)                    users#show
                PATCH  /users/:id(.:format)                    users#update
                PUT    /users/:id(.:format)                    users#update
                DELETE /users/:id(.:format)                    users#destroy

这是我以前的考试:

  test "should redirect update when not logged in" do
    patch user_path(@user), params: { user: { name: @user.name, email: @user.email } }
    assert_not flash.empty?
    assert_redirected_to login_url
  end

如果你们可能知道 - 我如何将assert_not flash.empty?转换为Rspec? 非常感谢提前!

1 个答案:

答案 0 :(得分:2)

首先,您无法在type: [:request, :controller]中使用多种类型。如果要编写请求规范,则必须指定命名路由(user_path)或url(" / users /:id")

require 'spec_helper'
describe UsersController, type: :request  do
  describe "#update" do
    let(:user) { create :user } # or use fixtures here

    # I'm just a bit confused here, why it should not update?
    it "doesn't update user" do
      patch user_path(user), email: "hello@example", name: "hello"
      expect(flash.empty?).to eq false
      expect(response).to redirect_to login_path
    end
  end
end