繁体   English   中英

不确定如何为 Create 方法 RSpec 测试传递数据

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM