Search code examples
ruby-on-railstestingrspec-rails

Test Rails CRUD methods with RSpec


I have a Post object in my Rails app that I am trying to write request tests for. I keep getting an ArgumentError: unknown keyword: post error. Here is my request spec:

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

I referenced the code from the Relish RSpec Docs


Solution

  • The API has changed so now you will need to pass in your post to params

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

    Notice that your reference specifies @rails_pre_5, while the example directly below it specifies @rails_post_5