【问题标题】:Test Rails CRUD methods with RSpec使用 RSpec 测试 Rails CRUD 方法
【发布时间】:2018-08-05 22:54:52
【问题描述】:

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

【问题讨论】:

  • 你使用的是什么 Rails 版本?
  • 我使用的是 Rails 5.1.6

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


【解决方案1】:

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

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

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

【讨论】:

  • 谢谢!那行得通。我不知道我是怎么错过的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
  • 1970-01-01
  • 2019-02-03
相关资源
最近更新 更多