标签: minitest

如何使用Minitest测试Rails路由?

我正在尝试将Minitest用于现有的Rails应用程序(3.2),但没有运气路由测试.我已经尝试过rspec语法(应该是route_to)和TestUnit语法(assert_routing),但没有运气.

有什么建议让这个工作?我需要包含的具体模块等?

谢谢

routing ruby-on-rails minitest

3
推荐指数
1
解决办法
3602
查看次数

是否可以使用MiniTest测试类方法(例如Foo.bar)?

如何Foo.bar在不测试bar方法行为(已在其他地方测试过)的情况下测试以下示例中调用的内容?

# Code
class Alpha
  def process
    Foo.bar
  end
end
Run Code Online (Sandbox Code Playgroud)

以下是我到目前为止的规范.不幸的是,这种方法抛出了"已经定义的类"警告,因为Foo已经在我的项目的其他地方定义过了.

 # Spec
 let(:alpha) { Alpha.new }
 let(:klass) { MiniTest::Mock.new }

 subject { alpha.process }

 it "calls Foo.bar" do
   klass.expect(:bar, '')     # Define method call expectation
   Foo = klass                # Redefine Foo as a mock object
   subject                    # Run method being tested
   klass.verify               # Confirm method was called
 end
Run Code Online (Sandbox Code Playgroud)

我不希望我的测试依赖于Foo类,因为这是一个外部依赖,我不想测试响应值Foo.bar,因为这可能会随意改变.

ruby minitest

3
推荐指数
1
解决办法
1810
查看次数

使用byebug进行调试时如何缩放堆栈?

我目前收到的错误如下:

NoMethodError: undefined method `debug' for nil:NilClass
    /mnt/hgfs/Dropbox/Company/Project/lib/project/misc.rb:23:in `debug'
    /mnt/hgfs/Dropbox/Company/Project/lib/project/validation/google_geocoding_validation_engine.rb:49:in `block in compare_addresses'
    /mnt/hgfs/Dropbox/Company/Project/lib/project/validation/google_geocoding_validation_engine.rb:43:in `each'
    /mnt/hgfs/Dropbox/Company/Project/lib/project/validation/google_geocoding_validation_engine.rb:43:in `compare_addresses'
    /mnt/hgfs/Dropbox/Company/Project/lib/project/validation/google_geocoding_validation_engine.rb:32:in `valid?'
    /mnt/hgfs/Dropbox/Company/Project/specs/project/validation/google_geocoding_validation_engine_spec.rb:56:in `block (2 levels) in <module:Validation>'
    /home/tomas/ruby2/lib/ruby/2.0.0/minitest/unit.rb:1301:in `run'
Run Code Online (Sandbox Code Playgroud)

我想我会尝试使用byebug来弄清楚对象的原因,nil因为它永远不应该是nil.我把它放在byebug错误的线上方:

def debug(&block)
  if @logger.nil?
    byebug
  end

  @logger.debug(@logger_name, &block)
end
Run Code Online (Sandbox Code Playgroud)

然后运行测试.我被放到了byebug调试界面,可以确认对象确实存在nil.问题是我无法爬上堆栈:

(byebug) up
*** Adjusting would put us beyond the oldest (initial) frame.
(byebug) down
*** Adjusting would put us beyond the newest (innermost) frame.
(byebug) backtrace
--> #0  AddressKit::Misc::Logging.debug(block#Proc) at /mnt/hgfs/Dropbox/Kvantel/Address Kit/lib/addresskit/misc.rb:25 …
Run Code Online (Sandbox Code Playgroud)

ruby minitest byebug

3
推荐指数
1
解决办法
2103
查看次数

错误"无法在RubyMine中加载此类文件 - 2.0/Console_ext"运行单元测试

我正在尝试开始在rails应用程序上运行样本ruby的单元测试,但每当我尝试运行单元测试时,RubyMine就会打印出以下错误

错误的屏幕截图

bundle install自从我添加gem "win32console", '1.3.0'到我的Gemfile后,我在运行时也遇到了同样的错误.这是文本形式的错误

      Exception message: cannot load such file -- 2.0/Console_ext
["D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/win32console-1.3.0-x86-mingw32/lib/Win32/Console.rb:12:in `require'", "D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/win32console-1.3.0-x86-mingw32/lib/Win32/Console.rb:12:in `rescue in <top (required)>'", "D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/win32console-1.3.0-x86-mingw32/lib/Win32/Console.rb:8:in `<top (required)>'", "D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/win32console-1.3.0-x86-mingw32/lib/Win32/Console/ANSI.rb:13:in `require'", "D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/win32console-1.3.0-x86-mingw32/lib/Win32/Console/ANSI.rb:13:in `<top (required)>'", "D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/win32console-1.3.0-x86-mingw32/lib/win32console.rb:1:in `require'", "D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/win32console-1.3.0-x86-mingw32/lib/win32console.rb:1:in `<top (required)>'", "D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/bundler-1.4.0.pre.2/lib/bundler/runtime.rb:76:in `require'", "D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/bundler-1.4.0.pre.2/lib/bundler/runtime.rb:76:in `block (2 levels) in require'", "D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/bundler-1.4.0.pre.2/lib/bundler/runtime.rb:72:in `each'", "D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/bundler-1.4.0.pre.2/lib/bundler/runtime.rb:72:in `block in require'", "D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/bundler-1.4.0.pre.2/lib/bundler/runtime.rb:61:in `each'", "D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/bundler-1.4.0.pre.2/lib/bundler/runtime.rb:61:in `require'", "D:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/bundler-1.4.0.pre.2/lib/bundler.rb:130:in `require'", "D:/Users/Justin/Projects/Rails/blog/config/application.rb:7:in `<top (required)>'", "D:/Users/Justin/Projects/Rails/blog/config/environment.rb:2:in `require'", "D:/Users/Justin/Projects/Rails/blog/config/environment.rb:2:in `<top (required)>'", "D:/Users/Justin/Projects/Rails/blog/test/test_helper.rb:2:in `require'", "D:/Users/Justin/Projects/Rails/blog/test/test_helper.rb:2:in `<top (required)>'", "D:/Users/Justin/Projects/Rails/blog/test/controllers/comments_controller_test.rb:1:in `require'", "D:/Users/Justin/Projects/Rails/blog/test/controllers/comments_controller_test.rb:1:in `<top (required)>'", "D:/Program Files (x86)/JetBrains/RubyMine 5.4.3.2.1/rb/testing/runner/tunit_or_minitest_in_folder_runner.rb:51:in `require'", "D:/Program …
Run Code Online (Sandbox Code Playgroud)

ruby rubymine minitest

3
推荐指数
1
解决办法
2046
查看次数

为什么我的Minitest测试不能并行运行?

我刚刚发现我的Minitest测试用例可以并行运行.我所要做的就是

require "minitest/hell"
Run Code Online (Sandbox Code Playgroud)

所以我做到了.不幸的是,我的测试运行与以前一样.一切都过去了,它需要的时间与通常一样多.我htop在运行我的测试套件时检查过,只使用了一个核心.

我在随机测试中设置断点以检查测试是否实际设置为并行运行:

(byebug) Minitest::Test.test_order
:parallel

发生什么了?


我的第一个假设是Minitest在决定产生多少进程时会计算CPU内核的数量.我有多个物理处理器(在虚拟机中),但每个处理器只有1个核心.我已经将我的VPS改为拥有两个物理处理器,每个处理器有4个核心,而我的测试仍然没有并行运行.

$ lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                8
On-line CPU(s) list:   0-7
Thread(s) per core:    1
Core(s) per socket:    4
Socket(s):             2
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 62
Stepping:              4
CPU MHz:               2600.000
BogoMIPS:              5200.00
Hypervisor vendor:     VMware
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              20480K
NUMA node0 CPU(s):     0-7

ruby parallel-processing minitest

3
推荐指数
1
解决办法
1927
查看次数

如何使用Minitest为Devise测试控制器sign_in

我是Rails测试的新手.

在线学习了一些教程后,我能够为Model设置和运行测试.

但是在尝试测试Controller时,测试失败了,因为它被重定向到登录页面.

我已经尝试过我可以在网上找到的所有指令来登录设计,但仍然无法登录并继续前进.

感谢有人可以提供帮助并给我一个前进的方向.

AwardedBidsControllerTest
  test_should_get_index                                           FAIL (0.45s)
MiniTest::Assertion:         Expected response to be a <:success>, but was <302>
Run Code Online (Sandbox Code Playgroud)

以下是我的设置

测试/ test_helper.rb中

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
require "minitest/reporters"
Minitest::Reporters.use!(
  Minitest::Reporters::SpecReporter.new,
  ENV,
  Minitest.backtrace_filter
)

class ActiveSupport::TestCase
  fixtures :all
  include FactoryGirl::Syntax::Methods
end

class ActionController::TestCase
  include Devise::TestHelpers
end
Run Code Online (Sandbox Code Playgroud)

测试/控制器/ awarded_bids_controller_test.rb

require "test_helper"

class AwardedBidsControllerTest < ActionController::TestCase
  test "should get index" do
    user = create(:user)
    sign_in user
    get :index
    assert_response :success
  end
end
Run Code Online (Sandbox Code Playgroud)

应用程序/控制器/ awarded_bids_controller.rb

class AwardedBidsController < …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails minitest devise ruby-on-rails-3

3
推荐指数
2
解决办法
4304
查看次数

Minitest:关联AR模型的装置导致ActiveRecord :: InvalidForeignKey:运行测试时PG :: ForeignKeyViolation

我遇到了Minitest和相关ActiveRecord模型灯具的问题(Rails.4.2.3).

以下是两种型号:

# vanguard_fund.rb
class VanguardFund < ActiveRecord::Base
  belongs_to :benchmark_fund
  ...
end

# benchmark_fund.rb
class BenchmarkFund < ActiveRecord::Base
  has_many :vanguard_funds
end
Run Code Online (Sandbox Code Playgroud)

非常直截了当.现在这里是固定装置:

# vanguard_funds.yml
vf1:
  name: Vanguard Fund 1
  benchmark_fund: bm1

# benchmark_funds.yml    
bm1:
  name: Benchmark Fund 1
Run Code Online (Sandbox Code Playgroud)

现在我在运行任何测试时遇到以下错误:

ERROR["test_#name_returns_the_name_of_the_VanguardFund", BaseTest, 2015-06-08 13:39:28 +0000]
 test_#name_returns_the_name_of_the_VanguardFund#BaseTest (1433770768.22s)
ActiveRecord::InvalidForeignKey:         ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR:  insert or update on table "vanguard_funds" violates foreign key constraint "fk_rails_994ab6fe75"
        DETAIL:  Key (benchmark_fund_id)=(479852872) is not present in table "benchmark_funds".
        : INSERT INTO "vanguard_funds" ("name", "created_at", "updated_at", "id", "benchmark_fund_id") VALUES …
Run Code Online (Sandbox Code Playgroud)

ruby postgresql ruby-on-rails fixtures minitest

3
推荐指数
1
解决办法
518
查看次数

在MiniTest中assert_predicate有什么用?

我有一个问题,了解assert_predicateMiniTest 的用处.它与assert_equal?何时想要使用这个断言有什么不同?我已经多次遇到它,但并没有真正得到它的确切含义.

ruby minitest

3
推荐指数
1
解决办法
1285
查看次数

Minitest - 测试不运行 - 没有Rails

我刚刚开始一个小型项目来模仿嘉年华的售票亭,其中一个指导方针是测试用户是否可以输入门票数量.该程序在控制台中运行,我最终(希望)想出了如何实现这个测试,感谢@ Stefan对这个问题的回答.

问题是,现在,当我运行测试文件时,minitest说:

0次运行,0次断言,0次失败,0次错误,0次跳过

当我尝试使用名称运行测试时,我得到相同的结果ruby path/to/test/file.rb --name method-name.我不确定这是不是因为我的代码仍然有问题,因为我是因为我错误地设置了minitest.我试图在SO上查找类似的问题,但大多数问题似乎涉及使用minitest和rails,我只是有一个普通的ruby项目.

这是我的测试文件:

gem 'minitest', '>= 5.0.0'
require 'minitest/spec'
require 'minitest/autorun'
require_relative 'carnival'

class CarnivalTest < MiniTest::Test
  def sample
    assert_equal(1, 1)
  end

  def user_can_enter_number_of_tickets
    with_stdin do |user|
      user.puts "2"
      assert_equal(Carnival.new.get_value, "2")
    end
  end

  def with_stdin
    stdin = $stdin                 # global var to remember $stdin
    $stdin, write = IO.pipe        # assign 'read end' of pipe to $stdin
    yield write                    # pass 'write end' to block
  ensure
    write.close                    # close pipe
    $stdin …
Run Code Online (Sandbox Code Playgroud)

ruby minitest

3
推荐指数
1
解决办法
1388
查看次数

无法加载此类文件-如果使用“ ruby​​”而不是“ rake test”运行测试用例,则为test_helper

如果我运行测试用例:

ruby test/models/chat_bot/option_test.rb
Run Code Online (Sandbox Code Playgroud)

我得到错误:

/home/anuja/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- test_helper (LoadError)
Run Code Online (Sandbox Code Playgroud)

如果我按以下方式运行测试,它将起作用:

rake test test/models/chat_bot/option_test.rb
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails minitest

3
推荐指数
2
解决办法
4564
查看次数