[英]Unsure how to pass data for Create method RSpec test
我正在為我的應用程序添加一些 Capybara/RSpec 測試,但在 controller 的創建方法中遇到了一些問題。
這是我的測試:
require 'rails_helper'
RSpec.describe Api::V1::CampgroundsController, type: :controller do
let(:campground_data) { FactoryBot.create :campground_1 }
let(:user) { FactoryBot.create :user_2 }
describe 'POST#create' do
it "admin should be able to create new campgrounds" do
sign_in user
before_count = Campground.count
post :create, params: campground_data, format: JSON
after_count = Campground.count
expect(after_count).to eq(before_count + 1)
end
end
end
營地 Controller - 創建方法:
def create
binding.pry
campground = Campground.new(campground_params)
if campground.save
render json: campground
else
render json: { errors: campground.errors.full_messages }
end
end
強參數:
def campground_params
params.require(:campground).permit([:name, :caption, :description, :location, :zip_code, :campground_link, :dogs_allowed, :electric_hookups, :water_hookups, :potable_water, :dump_station, :bathrooms, :showers])
end
當我運行測試時,它在這一行失敗: post:create, params: campground_data, format: JSON
。 它踢回的錯誤是#Campground:0x00007f9887ca4910`的undefined method
symbolize_keys'
我已經被這個問題困住了很長一段時間,經過數小時的谷歌搜索、搜索文檔和瀏覽舊帖子后,我仍然不確定問題出在哪里。 create 方法在生產中有效,但我不知道我需要做什么才能讓它在 RSpec 中工作。 我假設我在傳遞數據的方式上做錯了,但我不確定該怎么做。 我確實嘗試使用 FactoryBot 創建露營地,然后執行以下操作:
campground = {
name: campground.name,
caption: campground.caption,
description: campground.description,
location: campground.location,
zip_code: campground.zip_code,
campground_link: campground.campground_link,
dogs_allowed: campground.dogs_allowed,
electric_hookups: campground.electric_hookups,
water_hookups: campground.water_hookups,
potable_water: campground.potable_water,
dump_station: campground.dump_station,
bathrooms: campground.bathrooms,
showers: campground.showers }
當我這樣做時,我不再收到symbolize_keys
錯誤,並且我對 controller 進行了create
方法,但是當它到達這條線 campground campground = Campground.new(campground_params)
時,它會出現錯誤: ActionController::ParameterMissing: param is missing or the value is empty: campground
我主要嘗試這種方式作為測試,但我認為這不是創建數據以傳遞給 controller 的正確方法。 我覺得我的第一種方法應該有效,並且是更清潔的方法......我只是無法弄清楚我做錯了什么。
let(:campground_data) { FactoryBot.create :campground_1 }
已經在數據庫中創建了一個Campground
並返回了新實例。 您會收到錯誤消息undefined method 'symbolize_keys' for #Campground:0x00007f9887ca4910
,因為測試期望作為參數傳入的params
是 hash。
您可以使用FactoryBot.attributes_for
而不是FactoryBot.create
,它不會在數據庫中創建實例,而是返回 hash,其中包含可用於創建此類記錄的所有屬性。
我會這樣寫規范:
let(:campground_attrs) { FactoryBot.attributes_for :campground_1 }
let(:user) { FactoryBot.create :user_2 }
describe 'POST#create' do
it "admin should be able to create new campgrounds" do
sign_in user
expect {
post :create, params: { campground: campground_attrs }, format: JSON
}.to change { Campground.count }.by(1)
end
end
通過創建這樣的數據來修復它:
let!(:campground_data) { { campground: {
name: "Forked Lake Campground",
caption: "Rustic and remote campground. Almost all sites directly on lake. Many boat-in only.",
description: "Nice campground",
location: "New York",
zip_code: "12847",
campground_link: "https://www.dec.ny.gov/outdoor/24467.html",
dogs_allowed: true,
electric_hookups: false,
water_hookups: false,
potable_water: true,
dump_station: false,
bathrooms: true,
showers: false
} } }
這對我來說很有意義,盡管我真的不知道如何以對其他人有意義的方式來解釋它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.