Tag: minitest

使用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 ‘ /home/tomas/ruby2/lib/ruby/2.0.0/minitest/unit.rb:1301:in `run’ 我想我会尝试使用byebug来弄清楚为什么对象是nil因为它永远不应该是nil 。 我把byebug放在错误的线上: def debug(&block) if @logger.nil? byebug end @logger.debug(@logger_name, &block) end 然后运行测试。 我被放到了byebug调试界面,可以确认对象确实是nil 。 问题是我无法爬上堆栈: (byebug) up *** Adjusting would put us beyond the oldest (initial) frame. […]

如何在没有Rake的情况下运行完整的MiniTest套件?

已经看过这个问题了,或多或少反映了我目前运行整个套件的方式。 另外,我设置了以下rake任务: Rake::TestTask.new do |t| t.name = “spec:models” t.libs << "test" t.pattern = "spec/models/*_spec.rb" end 但是我注意到当我使用time rake spec:models运行它time rake spec:models ,它在大约2.36秒内完成。 如果我使用ruby /path/to/spec.rb运行该目录中的所有单独测试(目前所有测试都与ActiveRecord隔离 – 没有持久性,那么超快),他们的累计总用户时间是2.36秒,但我我还注意到,每个文件从开始到结束需要0.4个用户秒执行,MiniTest报告的实际“测试”时间要快得多(这样我的整个套件在加载依赖后,应该在低于0.15的情况下执行)是秒,而不是2.36秒)。 示例(对于一个spec文件): Started 11/11: 100% |======================================| Time: 00:00:00 Finished in 0.02223s 11 tests, 15 assertions, 0 failures, 0 errors, 0 skips ruby path/to/spec.rb 0.43s user 0.06s system 88% cpu 0.559 total […]

我如何用minitest中的块来模拟?

希望MiniTest人员的一个简单问题.. 我有一段代码,我将在这里浓缩成一个例子: class Foo def initialize(name) @sqs = Aws::SQS::Client.new @id = @sqs.create_queue( queue_name: name ).fetch(:queue_url) @poller = Aws::SQS::QueuePoller.new(@id) end def pick_first @poller.poll(idle_timeout: 60) do |message| process_msg(message) if some_condition(message) end end 我如何模拟/存根/其他东西,以便我可以通过some_condition()进行测试并可能使用process_msg() ? 即我想测试@poller.poll(idle_timeout: 60) do |message| 。 我试图用模拟轮询器来存根Aws::SQS::QueuePoller#new ,但是它没有让消息传递给|message| 刚回来.. 这就是我所拥有的,这是行不通的 : mockqueue = MiniTest::Mock.new mocksqs = MiniTest::Mock.new mocksqs.expect :create_queue, mockqueue, [Hash] mockpoller = MiniTest::Mock.new […]

在RSpec中的所有MiniTest测试中包含模块

在RSpec中,我可以在/spec/support/…创建辅助模块/spec/support/… module MyHelpers def help1 puts “hi” end end 并将其包含在每个规范中: RSpec.configure do |config| config.include(MyHelpers) end 并在我的测试中使用它,如下所示: describe User do it “does something” do help1 end end 如何在所有MiniTest测试中包含一个模块,而不必在每次测试中重复自己?

是否仍然建议使用Minitest测试Rails 4中的路由?

在Rails 3中,当在MiniTest中编写function测试时,我习惯于测试路由而不是测试我的控制器操作。 我从Rails测试指南中得到了这个想法- 第9节:测试路由 。 但是,在将我的应用程序升级到Rails 4之后,我注意到如果我没有使用一组适当的参数提供get|patch|post|delete方法,那么控制器操作测试本身已经开始涉及未知路由。 例如,给定路线: # config/routes.rb namespace “api” do namespace “v2”, defaults: { format: :json } do resources :users do resources :posts do resources :comments end end end end 和function测试: # test/controllers/api/v2/comments_controller_test.rb describe Api::V2::CommentsController it “does something” do get :index end end 在Rails 3中,上述方法可行。 但在Rails 4中,我收到了URL生成错误: ActionController :: UrlGenerationError:没有路由匹配{:action =>“index”,:controller =>“api […]

警卫为何停止?

我有一个rails应用程序,我只是把守卫和minitest和我的gaurd文件 guard ‘minitest’, :cli => ‘–drb –format doc –color’ do # with Minitest::Unit watch(%r|^test/(.*)\/?test_(.*)\.rb|) watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| “test/#{m[1]}test_#{m[2]}.rb” } watch(%r|^test/test_helper\.rb|) { “test” } # Rails watch(%r|^app/controllers/(.*)\.rb|) { |m| “test/functional/#{m[1]}_test.rb” } watch(%r|^app/helpers/(.*)\.rb|) { |m| “test/helpers/#{m[1]}_test.rb” } watch(%r|^app/models/(.*)\.rb|) { |m| “test/unit/#{m[1]}_test.rb” } end 但是当我跑卫时,我得到一个命令提示符 bundle exec guard 22:14:12 – INFO – Guard uses TerminalTitle to send notifications. […]

如何使用Ruby MiniTest :: Spec和Rails进行API集成测试?

我正在构建一个包含Rails API的应用程序,并希望使用Ruby MiniTest :: Spec进行测试。 设置它的好方法是什么? 例如,良好的目录组织,包含文件的好方法等? 我正在使用Rails 3 In Action一书中的指南,该指南使用了RSpec,并且有很多关于API的章节。 最大的变化是偏爱MiniTest :: Spec。

如何使用lib minitest或Test:Unit对unit testing进行着色?

我想在我的开发环境中让unit testing输出颜色。 但是,我无法在Linux(Debian和Ubuntu)上运行它。 当我包含以下库时: require ‘minitest/autorun’ require ‘minitest/unit’ require ‘minitest/pride’ 我明白了: /usr/local/rvm/gems/ruby-1.9.2-p136/gems/minitest-2.3.1/lib/minitest/pride.rb:35:in `’: undefined method `output’ for MiniTest::Unit:Class (NoMethodError) 由代码引起: MiniTest::Unit.output = PrideIO.new(MiniTest::Unit.output) 我见过一个有效的Rspec变种 。 不幸的是,我的Ruby知识还不足以看出差异。

URI :: InvalidURIError:错误的URI(不是URI?)测试Rails控制器

我得到URI::InvalidURIError测试Rails Home控制器: require ‘test_helper’ class HomeControllerTest < ActionDispatch::IntegrationTest test "should get index" do get :index assert_response :success end end 得到以下错误: E Error: HomeControllerTest#test_should_get_index: URI::InvalidURIError: bad URI(is not URI?): http://www.example.com:80index test/controllers/home_controller_test.rb:7:in `block in ‘ 堆栈如下: Rails 5.0.0.beta3 minitest (5.8.4)

用Minitest惯性模拟OpenURI.open_uri

我有调用OpenURI.open_uri代码,我想确认调用中使用的URI(因此存根不会对我有用),但也会拦截调用。 我希望不必为了测试目的而抽象地调用OpenURI.open_uri 。 我想出的东西似乎冗长而过于复杂。 under_test.rb require ‘open-uri’ class UnderTest def get_request(uri) open(uri).read end end test_under_test.rb require ‘minitest/autorun’ require ‘./lib/under_test’ class TestUnderTest < Mintest::Test def test_get_request @under_test = UnderTest.new mock_json = '{"json":[{"element":"value"}]}' uri = URI('https://www.example.com/api/v1.0?attr=value&format=json') tempfile = Tempfile.new('tempfile') tempfile.write(mock_json) mock_open_uri = Minitest::Mock.new mock_open_uri.expect(:call, tempfile, [uri]) OpenURI.stub :open_uri, mock_open_uri do @under_test.get_request('https://www.example.com/api/v1.0?attr=value&format=json' end mock_open_uri.verify end end 我是否误用或误解了Minitest的嘲笑? 跳舞的部分原因是我也在创建一个Tempfile以便我的read呼叫成功。 […]