使用 RSpec 测试 Rails CRUD 方法

Test Rails CRUD methods with RSpec

我的 Rails 应用程序中有一个 Post 对象,我正在尝试为其编写请求测试。我不断收到 ArgumentError: unknown keyword: post 错误。这是我的请求规范:

require 'rails_helper'

RSpec.describe "Post", :type => :request do
  describe '#create' do
    it "creates a Post and redirects to the Post's page" do
      get "/posts/new"
      expect(response).to render_template(:new)

### This is where the error is happening. On the `post` method.
      post "/posts", :post => {:title => "My Post", :content => "My post content"}

      expect(response).to redirect_to(assigns(:post))
      follow_redirect!

      expect(response).to render_template(:show)
      expect(response.body).to include("Post was successfully created.")
    end

    it "does not render a different template" do
      get "/posts/new"
      expect(response).to_not render_template(:show)
    end
  end
end

我引用了 Relish RSpec Docs

中的代码

API 已更改,因此现在您需要将 post 传递给 params

post "/posts", :params => { :post => {:title => "My Post", :content => "My post content"} }

请注意,您的引用指定了@rails_pre_5,而其正下方的示例指定了@rails_post_5