简体   繁体   中英

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? In the yml file for kigas I only have three samples for 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

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

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. 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. It expects +1 amount of the records you have. Check this answer: 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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