2017-02-01 66 views
1

修复模型试验&原始测试底部有迷你测试

我有哪些格式完全相同两次型试验,但只有一个作品。我是全新的微型测试或一般测试。我还阅读了迷你测试&的问题列表,但没有足够的技能或理解来找到可以解决发布的不同问题的修复方案。

目标:的工作在我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. 结算运行只是用3条继承路径为rails c test
  2. 单独测试夹具test_helper.rb
  3. 切换到测试文件中的integeration测试助手自行宣布
  4. 使用设置功能
  5. 多元化用手&由具有系统吐出了多元化的Campplay
  6. Capitolizing
  7. 使用.save!在rails c & rails c test
  8. 改变rails generate test_unit:model article title:string body:text例子&的Fileds符合我的模型rails generate test_unit:model Campplay campaign_id:integer player_id:integer
  9. 在测试文件更改测试文件名&类名来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 

宝石列表--local以 “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的版本 - 可能的情况应该是固定的 - 但现在我的工作清单&后卫不接受的版本只是多了一个好办法...

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摧毁模型Campplay
  2. 轨摹迁移dropCampplay
  3. 在迁移文件中没有一个drop_table :campplays
  4. rails g model Campplay campaign:references player:references
  5. 轨分贝:迁移
  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为他花时间在上面

+0

对不起被替换,但目前还不清楚你想要做什么。你的测试文件在哪里?你知道'rails c'打开一个控制台并且不运行测试吗? – sixty4bit

+0

Hi @ sixty4bit ...我认为控制台中的映射应该比测试文件更清楚地显示问题。我想让'.valid?'工作。此外,'rails c test'是我从rails文档中取出来的,它在rails控制台中加载测试env以用于提取错误消息 - 尽管它声称与运行测试文件相同。我会更新这个问题。 – Mirv

+0

可以显示广告系列和Campplay课程吗?什么错误“测试”无效 - 没有广告系列投放? – Gaston

回答

0

1)您应该更新红宝石

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 

有效的将失败名字不是因为因为身分证明。您应该添加:

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 
+0

是的,我已经在那里做了很多次campplay/Campplay的事情,并添加了删除验证是第一步。我复制并粘贴了多次工作测试 - 这也是为什么我在第八次刚刚开始离开县名作为var名称之后才终于找到了失败的原因。我确实同时升级了rvm和已安装的ruby 2.3.1 - 出于某种原因,它没有采取警惕......我将版本输出添加到页面底部...... – Mirv

+0

是否使我的代码有效? – Gaston

+0

对不起 - 应该明确地说不 - 不起作用。在这一点上,我将销毁模型,删除表并重新启动 - 它绝对必须是一个损坏的文件,因为我是唯一一个这种特殊情况发生的地方(如果它不是来自test_helper.rb的继承) ......但你已经提到过了。在shoulda匹配器上 - 我一直在看这个和rspec - 但在这一点上,我在5个框架和轨道上杂耍三种主要语言,必须让我每隔一天学习一个宝石......它会更频繁但没有他们对我来说很适合开箱... – Mirv