简体   繁体   English

Ruby on Rails控制器测试失败

[英]Ruby on Rails Controller Test Failure

Besides some other Failures I mainly get this one here: 除了一些其他的失败,我主要在这里得到这个:

FAIL["test_should_create_kiga", Minitest::Result, 1.6827512969975942]
test_should_create_kiga#Minitest::Result (1.68s)
    "Kiga.count" didn't change by 1.
    Expected: 4
      Actual: 3
    test/controllers/kigas_controller_test.rb:20:in `block in <class:KigasControllerTest>'

I am new to Ruby and I have no idea why it's expecting four kigas instead of three? 我是Ruby的新手,我不知道为什么要期待四个基加斯而不是三个? In the yml file for kigas I only have three samples for kigas: 在kigas的yml文件中,我只有三个kigas样本:

one:
name: MyString
city: MyString
postalcode: 1
streed: MyString
add_number: 1
capacity: 1
accessible: false
halalkit: false
koschakit: false
vegekit: false
vegankit: false
allday: false
user_id: 8
image_file_name: MyString
image_content_type: MyString
description: MyString

two:
name: MyString
city: MyString
postalcode: 1
streed: MyString
add_number: 1
capacity: 1
accessible: false
halalkit: false
koschakit: false
vegekit: false
vegankit: false
allday: false
user_id: 2
image_file_name: "kiga3.jpeg"
image_content_type: "image/jpeg"
description: MyString



three:
name: MyString
city: MyString
postalcode: 1
streed: MyString
add_number: 1
capacity: 1
accessible: false
halalkit: false
koschakit: false
vegekit: false
vegankit: false
allday: false
user_id: 2
image_file_name: "kiga3.jpeg"
image_content_type: "image/jpeg"
description: MyString

app/test/controllers/kigas_controller_test.rb 应用程序/测试/控制器/ kigas_controller_test.rb

require 'test_helper'

class KigasControllerTest < ActionDispatch::IntegrationTest
 setup do
  @kiga = kigas(:one)
  @other_kiga = kigas(:two)
 end
[...]
 test "should create kiga" do
  assert_difference('Kiga.count') do
   post kigas_url, params: { kiga: { accessible: @kiga.accessible, add_number: @kiga.add_number, allday: @kiga.allday, capacity: @kiga.capacity, city: @kiga.city, halalkit: @kiga.halalkit, koschakit: @kiga.koschakit, name: @kiga.name, postalcode: @kiga.postalcode, streed: @kiga.streed, vegankit: @kiga.vegankit, vegekit: @kiga.vegekit, user_id: @kiga.user_id } }
 end

  assert_redirected_to kiga_url(Kiga.last)
 end
[...]
end

app/controllers/kigas_controller.rb 应用程序/控制器/ kigas_controller.rb

class KigasController < ApplicationController
before_action :set_kiga, only: [:show, :edit, :update, :destroy]
before_action :logged_in_user
 def create
   @kiga = Kiga.new kiga_params
   @kiga.user = current_user
   @kiga.save
   respond_to do |format|
     if @kiga.save
       format.html { redirect_to @kiga, notice: 'Kiga was successfully created.' }
       format.json { render :show, status: :created, location: @kiga }
     else
       format.html { render :new }
       format.json { render json: @kiga.errors, status: :unprocessable_entity }
     end
   end
 end
end

So my Problem is, that I have no clue why the test is expecting four kigas in the create test because I have three samples in the .yml file. 所以我的问题是,我不知道为什么测试在创建测试中期望有四个kigas,因为.yml文件中有三个样本。 I have the same issue for some other objects in my app so maybe it is a fundamental problem I am facing here? 我的应用程序中的其他一些对象也遇到相同的问题,所以也许这是我在这里面临的一个基本问题? I am happy for any help! 我很乐意提供帮助!

It expects four kigas because it is the way assert_difference method works. 它期望四个kigas,因为这是assert_difference方法的工作方式。 It expects +1 amount of the records you have. 它期望您拥有的记录数量为+1。 Check this answer: https://stackoverflow.com/a/3348262/7956790 检查此答案: https : //stackoverflow.com/a/3348262/7956790
The thing is now, your post kigas_url is not creating a new kiga, maybe you have a certain validation in your Kiga model that is not passed? 现在,您的post kigas_url没有创建新的kiga,也许您在Kiga模型中有未通过的特定验证?

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

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