首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails请求规范post语法

rails请求规范post语法
EN

Stack Overflow用户
提问于 2017-07-31 23:37:55
回答 2查看 2.9K关注 0票数 1

我似乎不明白如何在请求规范测试中发出测试url的post请求,以下是测试代码

代码语言:javascript
复制
RSpec.describe "Certifications", type: :request do
  describe "denies public access" do

    it "for new certification form" do
        get new_certification_path
        expect(response).to redirect_to new_user_session_path
    end

    it "for creating certification" do
        certification_attributes = FactoryGirl.attributes_for :certification 

        expect {
            post "/certifications", { certification: certification_attributes }
        }.to_not change(Certification, :count)

        expect(response).to redirect_to new_user_session_path
    end
  end
end

这就给出了错误

代码语言:javascript
复制
  1) Certifications denies public access for creating certification
     Failure/Error: post "/certifications", { certification: certification_attributes }

 ArgumentError:
   unknown keyword: certification

我已经尝试过:certifications => certification_attributes,基本上不能理解如何传递参数。

正在测试的控制器是,只向这篇文章添加相关的方法。

代码语言:javascript
复制
class CertificationsController < ApplicationController
  skip_before_action :authenticate_user!, if: :skip_user_authentication
  before_action :set_certification, only: [:show, :edit, :update, :destroy]

  # GET /certifications
  # GET /certifications.json
  def index
    @certifications = Certification.all
  end

  # GET /certifications/1
  # GET /certifications/1.json
  def show
  end

  # GET /certifications/new
  def new
    @certification = Certification.new
  end

  # POST /certifications
  # POST /certifications.json
  def create
    @certification = Certification.new(certification_params)

    respond_to do |format|
      if @certification.save
        format.html { redirect_to @certification, notice: 'Certification was successfully created.' }
        format.json { render :show, status: :created, location: @certification }
      else
        format.html { render :new }
        format.json { render json: @certification.errors, status: :unprocessable_entity }
      end
    end
  end

  protected
    def skip_user_authentication
        request.format.json? && (action_name.eql?('show') || (action_name.eql?('index'))) 
    end 
end

我试图断言允许除certifications.jsoncertifications/1.json之外的所有方法不需要身份验证的行为,还有其他访问这些URL的测试,它们都通过了。确保它不允许任何其他请求的部分是我被卡住的地方。我在这个应用程序中使用Devise和Omnitauth Google OAuth2进行身份验证。

certification_attributes

代码语言:javascript
复制
{
 :name=>"Foundation Certification", 
 :description=>"Foundation Certification", 
 :terms=>"Foundation Certification", 
 :seo_meta_keywords=>["laaa", "lalala certifications"],
 :seo_meta_description=>"Foundation Certifications"
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-01 00:29:43

发送:params关键字下的请求参数:

代码语言:javascript
复制
post "/certifications", params: { certification: certification_attributes }
                        ^^^^^^
票数 7
EN

Stack Overflow用户

发布于 2017-07-31 23:45:06

看起来你已经设置了某种身份验证。您需要先让用户登录,然后再尝试发布。

将参数传递给post看起来没问题。在看不到控制器的情况下,很难说更多。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45420053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档