Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails控制器测试失败_Ruby On Rails_Ruby_Testing_Count_Controller - Fatal编程技术网

Ruby on rails RubyonRails控制器测试失败

Ruby on rails RubyonRails控制器测试失败,ruby-on-rails,ruby,testing,count,controller,Ruby On Rails,Ruby,Testing,Count,Controller,除了一些其他的失败之外,我主要在这里看到: 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_te

除了一些其他的失败之外,我主要在这里看到:

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>'
app/test/controllers/kigas\u controller\u 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
需要“测试助手”
类KigasControllerTest
app/controllers/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
class KigasController
所以我的问题是,我不知道为什么这个测试在create测试中需要四个kigas,因为我在.yml文件中有三个样本。我的应用程序中的一些其他对象也有同样的问题,所以这可能是我在这里面临的一个基本问题?
我很高兴能得到任何帮助!

它需要四个kigas,因为这是
断言差异
方法的工作方式。它需要+1个记录量。请检查此答案:
现在的问题是,你的
后kigas\u url
没有创建新的kiga,也许你的kiga模型中有某个验证没有通过

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