使用RSpec测试Rails CRUD方法

时间:2018-08-05 22:54:52

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

我的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

中的代码

1 个答案:

答案 0 :(得分:0)

API已更改,因此现在您需要将帖子传递到params

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

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