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

时间:2017-02-01 23:33:22

标签: ruby-on-rails ruby-on-rails-5 minitest

修复&底部的原始测试

我有两个模型测试,格式相同但只有一个有效。我是迷你测试或一般测试的新手。我还阅读了mini-test& amp;的问题列表。只是没有足够的技能或理解来找到适用于所发布的不同问题的修复。

目标:在我的CampplayTest.rb文件中工作.valid?或了解其失败的原因&我应该在它的位置使用什么(如设计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
  4. 的3个继承路径
  5. 在测试文件本身中切换到integeration test helper
  6. 使用设置功能
  7. 手工复数&amp;通过让系统吐出Campplay
  8. 的复数形式
  9. Capitolizing
  10. 使用.save!在rails c&amp; rails c test
  11. 更改rails generate test_unit:model article title:string body:text示例以匹配我的模型&amp;文件到rails generate test_unit:model Campplay campaign_id:integer player_id:integer
  12. 更改测试文件名称&amp;测试文件中的类名称为Campplay_Test
  13. 我的错误讯息

    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
    

    宝石列表 - 本地“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版本 - 情况可能应该修复 - 但我现在正在制作一份清单&amp;警卫不接受这个版本只是一个调整......

    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
    

    FIX

    很简单,它是损坏的文件......

    1. rails destroy model Campplay
    2. rails g migration dropCampplay
    3. 迁移文件中的
    4. 执行了drop_table :campplays
    5. 运行rails g model Campplay campaign:references player:references
    6. rails db:migrate
    7. copy&amp;粘贴我之前保存的模型代码
    8. copy&amp;已保存的原始测试代码已粘贴
    9. 最终测试文件的副本......

      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 个答案:

答案 0 :(得分:0)

1)你应该更新ruby

  

你的Ruby是:2.3.0(buggy)。建议:升级到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