• 谢谢大腿荐书,先大致过完手上这本就去看🌹

  • @piecehealth @Rei @tinyfeng 感谢各位大腿的帮助,刚刚已经成功解决了问题

    问题所在

    我看的是《Ruby on Rails 教程》样章试读
    这个链接是之前官网的样章试读链接,直接从网页打开
    这个版本中的样章中的 rails 版本是 5.1.2
    现在从官网链接进去点击样章试读是下载一个 pdf,其中 rails 的版本是 5.1.6(中间也就差了一两天)
    在各章给出的 Gemfile 文件中也只有 rails 的版本这一个不同gem 'rails', '5.1.6'
    在将 Gemfile 文件根据最新的电子书修改后,已经没有了本贴所描述的问题

    再次感谢三位大腿的帮助
    我已购买此电子书以便接收它的最新更新
    (吐槽:之前买的实体书上面 rails 的版本是 5.0.0)

    如果没有更多讨论,我将与下周之前关闭这个贴子,谢谢🌹

  • 抱住大佬的大腿哭😭

  • 我这个测试就是为了得到失败,但是得到的失败信息不对,大佬可以看下 16 楼我重现的结果,似乎是 gem 包的问题😭

  • @piecehealth @Rei 我刚刚重现这个问题去了,发现问题好像是某个 gem 包的问题(但是我才刚入门不懂 gem 包怎么选啊都是按教程来的😭

    $ rails _5.1.2_ new test_app
    $ cd test_app
    $ rails generate controller Pages home help
    $ rails test
    

    一直到这里都没问题(虽然会提示要跑rails db:migrate但是不影响 test 结果)
    然后我在 test/controllers/pages_controller_test.rb 中添加两个失败测试(后两个)

    require 'test_helper'
    
    class PagesControllerTest < ActionDispatch::IntegrationTest
      test "should get home" do
        get pages_home_url
        assert_response :success
      end
    
      test "should get help" do
        get pages_help_url
        assert_response :success
      end
    
      test "should get about" do
        get pages_about_url
        assert_response :success
      end
    
      test "should get contact" do
        get pages_contact_url
        assert_response :success
      end
    end
    

    再运行rails test可以得到预期的结果

    sws@Ubuntu:~/ruby_on_rails/environment/bbb_app$ rails test
    Running via Spring preloader in process 6083
    /home/sws/ruby_on_rails/environment/bbb_app/db/schema.rb doesn't exist yet. Run `rails db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /home/sws/ruby_on_rails/environment/bbb_app/config/application.rb to limit the frameworks that will be loaded.
    Run options: --seed 34068
    
    # Running:
    
    .E
    
    Error:
    PagesControllerTest#test_should_get_about:
    NameError: undefined local variable or method `pages_about_url' for #<PagesControllerTest:0x0000000005b8ecf0>
        test/controllers/pages_controller_test.rb:15:in `block in <class:PagesControllerTest>'
    
    
    bin/rails test test/controllers/pages_controller_test.rb:14
    
    E
    
    Error:
    PagesControllerTest#test_should_get_contact:
    NameError: undefined local variable or method `pages_contact_url' for #<PagesControllerTest:0x000000000559a560>
        test/controllers/pages_controller_test.rb:20:in `block in <class:PagesControllerTest>'
    
    
    bin/rails test test/controllers/pages_controller_test.rb:19
    
    .
    
    Finished in 0.358412s, 11.1603 runs/s, 5.5802 assertions/s.
    4 runs, 2 assertions, 0 failures, 2 errors, 0 skips
    

    但是当我按教程要求更新 Gemfile 之后
    gemfile(我去掉了注释)

    source 'https://rubygems.org'
    
    git_source(:github) do |repo_name|
      repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
      "https://github.com/#{repo_name}.git"
    end
    
    gem 'rails', '~> 5.1.2'
    gem 'sqlite3'
    gem 'puma', '~> 3.7'
    gem 'sass-rails', '~> 5.0'
    gem 'uglifier', '>= 1.3.0'
    gem 'coffee-rails', '~> 4.2'
    gem 'turbolinks', '~> 5'
    gem 'jbuilder', '~> 2.5'
    
    group :development, :test do
      gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
      gem 'capybara', '~> 2.13'
      gem 'selenium-webdriver'
    end
    
    group :development do
      gem 'web-console', '>= 3.3.0'
      gem 'listen', '>= 3.0.5', '< 3.2'
      gem 'spring'
      gem 'spring-watcher-listen', '~> 2.0.0'
    end
    
    # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
    gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
    

    修改后的 gemfile

    source 'https://rubygems.org'
    
    gem 'rails', '5.1.2'
    gem 'puma', '3.9.1'
    gem 'sass-rails', '5.0.6'
    gem 'uglifier', '3.2.0'
    gem 'coffee-rails', '4.2.2'
    gem 'jquery-rails', '4.3.1'
    gem 'turbolinks', '5.0.1'
    gem 'jbuilder', '2.7.0'
    
    group :development, :test do
      gem 'sqlite3', '1.3.13'
      gem 'byebug', '9.0.6', platform: :mri
    end
    
    group :development do
      gem 'web-console', '3.5.1'
      gem 'listen', '3.0.8'
      gem 'spring', '2.0.2'
      gem 'spring-watcher-listen', '2.0.1'
    end
    
    group :test do
      gem 'rails-controller-testing', '1.0.2'
      gem 'minitest-reporters', '1.1.14'
      gem 'guard', '2.13.0'
      gem 'guard-minitest', '2.4.4'
    end
    
    group :production do
      gem 'pg', '0.20.0'
    end
    
    # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
    gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
    

    更新 gem 包

    $ bundle install --without production
    $ bundle update
    

    这时候再跑rails test就不能正常运行了

    sws@Ubuntu:~/ruby_on_rails/environment/aaa_app$ rails test
    Running via Spring preloader in process 25721
    /home/sws/ruby_on_rails/environment/aaa_app/db/schema.rb doesn't exist yet. Run `rails db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /home/sws/ruby_on_rails/environment/aaa_app/config/application.rb to limit the frameworks that will be loaded.
    Run options: --seed 13155
    
    # Running:
    
    E
    
    Error:
    PagesControllerTest#test_should_get_about:
    NameError: undefined local variable or method `pages_about_url' for #<PagesControllerTest:0x0000000003caa708>
        test/controllers/pages_controller_test.rb:15:in `block in <class:PagesControllerTest>'
    
    
    /home/sws/.rvm/gems/ruby-2.4.4/gems/railties-5.1.2/lib/rails/test_unit/reporter.rb:70:in `method': undefined method `test_should_get_about' for class `Minitest::Result' (NameError)
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/railties-5.1.2/lib/rails/test_unit/reporter.rb:70:in `format_rerun_snippet'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/railties-5.1.2/lib/rails/test_unit/reporter.rb:23:in `record'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-reporters-1.1.14/lib/minitest/minitest_reporter_plugin.rb:21:in `block in record'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-reporters-1.1.14/lib/minitest/minitest_reporter_plugin.rb:20:in `each'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-reporters-1.1.14/lib/minitest/minitest_reporter_plugin.rb:20:in `record'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-5.11.3/lib/minitest.rb:803:in `block in record'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-5.11.3/lib/minitest.rb:802:in `each'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-5.11.3/lib/minitest.rb:802:in `record'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-5.11.3/lib/minitest.rb:334:in `run_one_method'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-5.11.3/lib/minitest.rb:321:in `block (2 levels) in run'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-5.11.3/lib/minitest.rb:320:in `each'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-5.11.3/lib/minitest.rb:320:in `block in run'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-5.11.3/lib/minitest.rb:360:in `on_signal'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-5.11.3/lib/minitest.rb:347:in `with_info_handler'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-5.11.3/lib/minitest.rb:319:in `run'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/railties-5.1.2/lib/rails/test_unit/line_filtering.rb:11:in `run'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-5.11.3/lib/minitest.rb:159:in `block in __run'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-5.11.3/lib/minitest.rb:159:in `map'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-5.11.3/lib/minitest.rb:159:in `__run'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-5.11.3/lib/minitest.rb:136:in `run'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/railties-5.1.2/lib/rails/test_unit/minitest_plugin.rb:77:in `run'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/minitest-5.11.3/lib/minitest.rb:63:in `block in autorun'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/spring-2.0.2/lib/spring/application.rb:171:in `fork'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/spring-2.0.2/lib/spring/application.rb:171:in `serve'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/spring-2.0.2/lib/spring/application.rb:141:in `block in run'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/spring-2.0.2/lib/spring/application.rb:135:in `loop'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/spring-2.0.2/lib/spring/application.rb:135:in `run'
        from /home/sws/.rvm/gems/ruby-2.4.4/gems/spring-2.0.2/lib/spring/application/boot.rb:19:in `<top (required)>'
        from /home/sws/.rvm/rubies/ruby-2.4.4/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:59:in `require'
        from /home/sws/.rvm/rubies/ruby-2.4.4/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:59:in `require'
        from -e:1:in `<main>'
    
  • 那就是了,现在就是整个项目只有一个 test.rb,里面有 4 个 test,两个成功两个失败
    但是 rails test 在跑到第一个失败的时候就告诉我“嘿,你这里没写”,然后第四个 test 不跑了
    不告诉我“你跑了 4 个测试,错了两个” 咋办啊😭

  • test case 是指的一个 xxx_test.rb 的文件吗,
    还是指的一个 test xxx end?

    我这里只有一个 test.rb 的文件,其它目录只有一个 .keep 文件
    这个文件里面只有脚手架自动添加的两个 test xxx end,和一个预期就是失败的 test about xxx end
    (后面还添了第二个失败的 test)

  • 没有省略的说明。
    主要问题在教程中说如果写 3 个测试,如果有 1 个错误的话会告诉我

    3 tests, 2 assertions, 0 failures, 1 errors, 0 skips

    但是没有这句话,当我写 4 个测试,已知有 2 个错误,那么 rails test 的结果应该有一句话

    4 tests, 2 assertions, 0 failures, 2 errors, 0 skips

    但是也没有这句话,并且报错和写 3 错 1 的输出是一样的,也就是在问题中有贴报错信息的部分
    那么如果我写了 100 个测试,其中有 10 个失败了,那么我也只能得到其中的一个失败
    rails test 应该有总结性的语句告诉我我跑了 100 个失败了 10 个,但是并没有
    它现在只告诉我“你这个页面没写”,不会告诉我另外 9 个错误的事情

  • 是的我知道失败是符合预期的,但是失败输出的结果不符合预期。
    我只使用脚手架添加了 home 和 help,但是在 test 中加入了 about
    按照教程中给出的结果,rails test 应该有一句话告诉我

    3 tests, 2 assertions, 0 failures, 1 errors, 0 skips

    但实际上没有这句话,于是我做了测试

    在只使用脚手架添加 home 和 help 的情况下,我在 test 中额外加入了 about 和 contact 两个的失败测试
    预期的 rails test 的结果应该是

    4 tests, 2 assertions, 0 failures, 2 errors, 0 skips

    但实际上在检测到 about 路由未定义的时候就不会再检测到 contact 的错误了
    也就是没有这句总结性的语句(来表示我跑了多少个测试)的输出
    而 rails test 输出的结果和只添加一个 about 的检测一样(都在上面给出了)
    只会说 about 未定义

    我下面那个回复可能组织语言更加清楚

  • 抱歉没看明白,测试的父类确实是这个,但是代码里面本身就是static_pages_about_url

    require 'test_helper'
    class StaticPagesControllerTest < ActionDispatch::IntegrationTest
    ...
      test 'should get about' do
        get static_pages_about_url
        assert_response :success
      end
    end