我似乎無法讓我的頭在如何使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.json
或certifications/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"
}
哪條線導致錯誤? –
另外,'certification_params'是什麼樣的? –
@MattGibson添加錯誤的詳細信息 –