使用迷你测试进行模型测试

Model testing with mini-test

修复底部的原始测试

我有两个格式相同的模型测试,但只有一个有效。我对迷你测试或一般测试不熟悉。我还阅读了小型测试的问题列表,只是没有足够的技能或理解来找到适用于已发布的不同问题的修复程序。

目标: 在我的 CampplayTest.rb 文件中工作 .valid? 或了解它失败的原因以及我应该在它的位置使用什么(如devise 的 blowmage 说 .valid? 应该用来代替 .save!)

编辑:显然这是我的模型 - 不确定它是如何无效的 - 请参阅下面的原始错误

此处测试文件失败...

require 'test_helper'

class CampplayTest < ActiveSupport::TestCase

  # Test one missing from pair of ID
  test 'invalid - no campaign' do
    county = campplay.new(player_id: 1)
    refute county.valid?, 'Campplay passed without a name'
  end

end

正在此处传递测试文件...

require "test_helper"

class County_Test < ActiveSupport::TestCase
  def setup
    @county = County.create(name: "Example Item")
  end
 test 'valid county' do
    assert @county.valid?, 'county must have name'
  end

  # Test duplicate
  test 'invalid - duplicate county' do
    county = County.new(name: "Example Item")
    refute county.valid?, 'county passed without a name'
  end

end

我试过了...

  1. 清除固定装置 运行 仅在 rails c test
  2. 中单独测试
  3. test_helper.rb 使用 3 个继承路径
  4. 在测试文件本身中切换到集成测试助手
  5. 使用设置功能
  6. 手动复数化以及让系统吐出 Campplay
  7. 的复数化
  8. 首都化
  9. 使用.save!在 rails c & rails c test
  10. 更改 rails generate test_unit:model article title:string body:text 示例以匹配我的模型和文件到 rails generate test_unit:model Campplay campaign_id:integer player_id:integer
  11. 正在将测试文件名和测试文件中的 class 名称更改为 Campplay_Test

我的错误信息...

2.3.1 :006 >     c = Campplay.new(player_id: "1", campaign_id: "1")                                                                                                   
 => #<Campplay id: nil, campaign_id: 1, player_id: 1, created_at: nil, updated_at: nil> 
2.3.1 :007 > c.valid?
NoMethodError: undefined method `Campplay' for #<Campplay:0x00000004630890>
Did you mean?  campaign
        from /usr/local/rvm/gems/ruby-2.3.1/gems/activemodel-5.0.0/lib/active_model/attribute_methods.rb:433:in `method_missing'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/activemodel-5.0.0/lib/active_model/validator.rb:149:in `block in validate'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/activemodel-5.0.0/lib/active_model/validator.rb:148:in `each'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/activemodel-5.0.0/lib/active_model/validator.rb:148:in `validate'
(I truncated here to try & not spam people)

test_helper.rb

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"

class ActiveSupport::TestCase
  fixtures :all
end

class ActionController::TestCase
  include Devise::Test::ControllerHelpers
end

我的 Campplay 模型 ...

class Campplay < ApplicationRecord
  belongs_to :campaign
  belongs_to :player

  validates :campaign_id, presence: true
  validates :player_id, presence: true

end

Gem 列表 --local with "test"

guard-minitest (2.4.6)
minitest (5.10.1, 5.8.3)
minitest-capybara (0.8.2)
minitest-color (0.0.2)
minitest-metadata (0.6.0)
minitest-rails (3.0.0)
minitest-rails-capybara (3.0.0)
rack-test (0.6.3)
rails-dom-testing (2.0.2)
test-unit (3.1.5)

全防护档...

     bundle exec guard
RubyDep: WARNING: Your Ruby is outdated/buggy.
RubyDep: WARNING: Your Ruby is: 2.3.0 (buggy). Recommendation: upgrade to 2.3.1.
RubyDep: WARNING: (To disable warnings, see:http://github.com/e2/ruby_dep/wiki/Disabling-warnings )
12:46:03 - INFO - Guard::Minitest 2.4.6 is running, with Minitest::Unit 5.10.1!
12:46:04 - INFO - Guard is now watching at '/home/ubuntu/workspace/basicB'
12:46:07 - INFO - Running: test/models/campplay_test.rb
RubyDep: WARNING: Your Ruby is outdated/buggy.
RubyDep: WARNING: Your Ruby is: 2.3.0 (buggy). Recommendation: upgrade to 2.3.1.
RubyDep: WARNING: (To disable warnings, see:http://github.com/e2/ruby_dep/wiki/Disabling-warnings )
RubyDep: WARNING: Your Ruby is outdated/buggy.
RubyDep: WARNING: Your Ruby is: 2.3.0 (buggy). Recommendation: upgrade to 2.3.1.
RubyDep: WARNING: (To disable warnings, see:http://github.com/e2/ruby_dep/wiki/Disabling-warnings )
Run options: --seed 24359

# Running:

E

Error:
CampplayTest#test_invalid_-_no_campaign:
NameError: undefined local variable or method `campplay' for #<CampplayTest:0x00000003e05710>
Did you mean?  campplays
    test/models/campplay_test.rb:14:in `block in <class:CampplayTest>'


bin/rails test test/models/campplay_test.rb:13

E

Finished in 0.275098s, 3.6351 runs/s, 0.0000 assertions/s.

1 runs, 0 assertions, 0 failures, 1 errors, 0 skips

[1] guard(main)> 

我的 Campplays 架构...

  create_table "campplays", force: :cascade do |t|
    t.integer  "campaign_id"
    t.integer  "player_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.index ["campaign_id"], name: "index_campplays_on_campaign_id"
    t.index ["player_id"], name: "index_campplays_on_player_id"
  end

Rails 版本 - 情况可能应该得到解决 - 但我现在正在制作一份清单 & guard 不接受该版本只是又一个调整...

mirv:~/workspace (master) $ cd basicB
mirv:~/workspace/basicB (master) $ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
mirv:~/workspace/basicB (master) $ rvm install 2.3.1
Already installed ruby-2.3.1.
To reinstall use:

    rvm reinstall ruby-2.3.1

修复

很简单,就是文件损坏了……

  1. rails 摧毁模型 Campplay
  2. rails g 迁移 dropCampplay
  3. 在迁移文件中做了 drop_table :campplays
  4. 运行 rails g model Campplay campaign:references player:references
  5. rails db:migrate
  6. 复制并粘贴我之前保存的模型代码
  7. 复制并粘贴我保存的原始测试代码

最终测试文件的副本...

require 'test_helper'

class CampplayTest < ActiveSupport::TestCase

  test 'valid Campplay' do
    cp = Campplay.new(player_id: "1", campaign_id: "1")
    assert cp.valid?, 'Campplay must have player_id'
  end

  # Test one missing from pair of ID
  test 'invalid - no campaign' do
    cp = Campplay.new(player_id: 1)
    refute cp.valid?, 'Campplay passed without a campaign_id'
  end

  test 'invalid - no player' do
    cp = Campplay.new(campaign_id: 1)
    refute cp.valid?, 'Campplay passed without a name'
  end

end

我把修复归功于@gaston,因为他花了很多时间

1) 你应该更新 ruby

Your Ruby is: 2.3.0 (buggy). Recommendation: upgrade to 2.3.1.

2) 错误是大写 C:

class CampplayTest < ActiveSupport::TestCase

  test 'invalid - no campaign' do
    county = Campplay.new(player_id: 1)       #you should change county to campplay
    refute county.valid?, 'Campplay passed without a name'
  end

end

3)但是,测试中出现了一些错误。测试会通过,但是另外一个原因,Campplay需要两个id:

validates :campaign_id, presence: true
validates :player_id, presence: true

验证失败是因为id,不是因为名字。您应该添加:

validates :name, presence: true #optional  ,length: { minimum: 1 }

4)我推荐 shoulda-matches

这样做,整个测试将被替换为

class CampplayTest < ActiveSupport::TestCase

  should validate_presence_of(:name)
  should validate_presence_of(:player_id)
  should validate_presence_of(:campaign_id)
  should belong_to(:campaign)
  should belong_to(:player)
end