2017-07-31 23 views
1

我似乎无法让我的头在如何使POST请求为要求规范测试中的一个网址,这里的测试代码轨要求规范后的语法

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 

这给错误

1) Certifications denies public access for creating certification 
    Failure/Error: post "/certifications", { certification: certification_attributes } 

ArgumentError: 
    unknown keyword: certification 

我试过:certifications => certification_attributes,基本上无法让我的头如何通过params。

被测试的控制器只在本文中添加相关方法。

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所有方法不要求身份验证的行为,有其访问这些网址,然后他们通过其他测试。确保它不允许任何其他请求的部分是我卡住的地方。我正在使用Devise和Omnitauth Google OAuth2在此应用程序中进行身份验证。

certification_attributes

{ 
:name=>"Foundation Certification", 
:description=>"Foundation Certification", 
:terms=>"Foundation Certification", 
:seo_meta_keywords=>["laaa", "lalala certifications"], 
:seo_meta_description=>"Foundation Certifications" 
} 
+0

哪条线导致错误? –

+0

另外,'certification_params'是什么样的? –

+0

@MattGibson添加错误的详细信息 –

回答

2

发送请求参数:

post "/certifications", params: { certification: certification_attributes } 
         ^^^^^^ 
+0

修复它,谢谢! –

0

看起来你有某种身份验证设置。在尝试POST之前,您需要先登录用户。

传递参数到post看起来没问题。狡猾的说更多,而不看你的控制器。下:params关键字

+0

这就是测试的意图,如果访问时没有有效的会话,它应该发送用户登录页面。 –