简体   繁体   English

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

[英]Unsure how to pass data for Create method RSpec test

I'm working on adding some Capybara/RSpec tests for my app and I'm having some issues with one for the create method of a controller.我正在为我的应用程序添加一些 Capybara/RSpec 测试,但在 controller 的创建方法中遇到了一些问题。

This is my test:这是我的测试:

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

Campground Controller - Create Method:营地 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

Strong Params:强参数:

 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

When I run the test it fails on this line: post:create, params: campground_data, format: JSON .当我运行测试时,它在这一行失败: post:create, params: campground_data, format: JSON The error it kicks back is undefined method symbolize_keys' for #Campground:0x00007f9887ca4910`它踢回的错误是#Campground:0x00007f9887ca4910`的undefined method symbolize_keys'

I've been stuck on this problem for a stupid amount of time and after hours of googling, scouring the documentation, and looking through old posts I'm still unsure what the problem.我已经被这个问题困住了很长一段时间,经过数小时的谷歌搜索、搜索文档和浏览旧帖子后,我仍然不确定问题出在哪里。 The create method works in production but I can't figure out what I need to do to get this to work in RSpec. create 方法在生产中有效,但我不知道我需要做什么才能让它在 RSpec 中工作。 I'm assuming I'm doing something wrong about how I'm passing in the data but I'm not sure how else to do it.我假设我在传递数据的方式上做错了,但我不确定该怎么做。 I did try creating the campground with FactoryBot and then doing this:我确实尝试使用 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 }

I no longer get the symbolize_keys error when I do it this way and I make to the create method on the controller but when it gets to this line campground = Campground.new(campground_params) it errors out with the error: ActionController::ParameterMissing: param is missing or the value is empty: campground I mostly tried it this way as a test and I don't think it's the right way to create the data to pass to the controller anyway.当我这样做时,我不再收到symbolize_keys错误,并且我对 controller 进行了create方法,但是当它到达这条线 campground campground = Campground.new(campground_params)时,它会出现错误: ActionController::ParameterMissing: param is missing or the value is empty: campground我主要尝试这种方式作为测试,但我认为这不是创建数据以传递给 controller 的正确方法。 I feel like my first approach should work and is the cleaner way of doing it...I just can't figure out what I'm doing wrong.我觉得我的第一种方法应该有效,并且是更清洁的方法......我只是无法弄清楚我做错了什么。

let(:campground_data) { FactoryBot.create :campground_1 }

already creates a Campground in the database and returns the new instance.已经在数据库中创建了一个Campground并返回了新实例。 You get the error message undefined method 'symbolize_keys' for #Campground:0x00007f9887ca4910 because the test expects the argument passed in as params to be a hash.您会收到错误消息undefined method 'symbolize_keys' for #Campground:0x00007f9887ca4910 ,因为测试期望作为参数传入的params是 hash。

Instead of FactoryBot.create you could use FactoryBot.attributes_for which does not create an instance in the database but returns a hash containing all the attributes that can be used to create such a record.您可以使用FactoryBot.attributes_for而不是FactoryBot.create ,它不会在数据库中创建实例,而是返回 hash,其中包含可用于创建此类记录的所有属性。

I would write the spec like this:我会这样写规范:

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

Fixed it by creating the data like this:通过创建这样的数据来修复它:

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
  } } }

Which mostly makes sense to me although I don't really know how to explain it in a way that would make sense to others.这对我来说很有意义,尽管我真的不知道如何以对其他人有意义的方式来解释它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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