我正在为我的应用程序添加一些Capybara/RSpec测试,我有一个控制器的创建方法的一些问题。
这是我的测试:
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
露营控制器-创建方法:
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
。它返回的错误是undefined method
symbolize_keys' for #Campground:0x00007f9887ca4910 '
我被这个问题困了很长一段时间,经过几个小时的谷歌搜索,搜索文档,查看旧帖子,我仍然不确定问题是什么。创建方法在生产中工作,但我不知道我需要做什么才能让它在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
错误,我在控制器上使用create
方法,但是当它到达这行campground = Campground.new(campground_params)
时,它错误了错误:ActionController::ParameterMissing: param is missing or the value is empty: campground
我主要以这种方式尝试作为测试,我不认为这是创建数据传递给控制器的正确方法。我觉得我的第一种方法应该是有效的,而且是更干净的方法……我就是不知道我做错了什么。
let(:campground_data) { FactoryBot.create :campground_1 }
已经在数据库中创建了一个Campground
,并返回了新的实例。您将得到错误消息undefined method 'symbolize_keys' for #Campground:0x00007f9887ca4910
,因为测试期望作为params
传入的参数是哈希值。
代替FactoryBot.create
,你可以使用FactoryBot.attributes_for
,它不会在数据库中创建一个实例,但返回一个包含所有属性的哈希值,可以用来创建这样一个记录。
我会这样写规范:
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
} } }
这对我来说基本上是有意义的,尽管我真的不知道如何用一种别人也能理解的方式来解释。