【问题标题】:RSpec test pass even when test.log shows ActionNotFound即使 test.log 显示 ActionNotFound,RSpec 测试也通过
【发布时间】:2014-04-02 09:07:35
【问题描述】:

我遵循 M Hartl 的“Ruby on Rails 教程”,第 3.2.2 章

当我将 /static_pages/about 添加到 routes.rb 时,即使在 static_pages_controller.rb 中未定义“about”操作,rspec 测试也会通过。

快把我逼疯了!

我正在使用 ruby​​-2.1.1、rspec-rails 2.14.2、Capybara 2.1.0

static_pages_spec.rb:

require 'spec_helper'
describe "StaticPages" do
  describe "About" do
    it "should have the content 'About'" do
      visit '/static_pages/about'
      expect(page).to have_content('About')
    end
  end
end

routes.rb:

RortSampleApp::Application.routes.draw do
  get "static_pages/about"
end

static_pages_controller.rb:

class StaticPagesController < ApplicationController
end

运行 rspec 测试和结果的命令:

$ rspec spec/requests/static_pages_spec.rb 
.

Finished in 0.02604 seconds
1 example, 0 failures

Randomized with seed 61832

还有 test.log:

   (0.1ms)  begin transaction
Started GET "/static_pages/about" for 127.0.0.1 at 2014-04-02 01:59:45 -0700

AbstractController::ActionNotFound - The action 'about' could not be found for       StaticPagesController:
  actionpack (4.0.4) lib/abstract_controller/base.rb:131:in `process'
  actionpack (4.0.4) lib/abstract_controller/rendering.rb:44:in `process'

【问题讨论】:

    标签: ruby-on-rails ruby rspec capybara


    【解决方案1】:

    在 M.Hartl 的 Ruby on Rails 教程的第二版中,Gemfile 的 Capybara 版本为 1.1.2。如果您在 Gemfile 中更改 Capybara 的版本,我希望它会更好。

    group :test do
      gem 'capybara', '1.1.2'
     ...
    end
    

    第二版的 Gemfile 中的 rspec-rails gem 版本也不同。因此,如果上述更改不起作用,请尝试以下 rspec-rails 版本:2.11.0,以及 Capybara 版本更改。 (我在使用 Capybara 2.1.0 和 2.2.0 版本时遇到了困难)

    group :development, :test do
    ..
      gem 'rspec-rails', '2.11.0'
    ..
    end
    

    Gemfile for tutorial's 2nd edition

    【讨论】:

    • 试过了,但也没有用。结果和以前一样。
    • 对不起,我刚刚意识到我的测试工作正常(我应该首先进行适当的检查)。但仔细观察后,我发现我使用了不同的 Capybara 版本来使其工作:gem 'capybara', '~> 1.1.2'。我遇到了 2.1.0 和 2.2.0 版本的问题。
    • 由于 capybara 1.1.2 不再可用,我使用 1.1.4;但不工作。然后我把 rspec-rails 改成 2.11.0,还是不行。而且,我还发现了一个奇怪的问题,在我从 static_pages_controller.rb 中删除 'about' 操作,并且 app/view/static_pages/about.html.erb 存在之后,我仍然可以从浏览器访问 /static_pages/about !我使用的是 Ruby 2.1.1,有什么问题吗?
    • 是的,因为使用 gem 'capybara', '~> 1.1.2',我一开始就安装了 1.1.4 版本。红宝石版本 2.1.0。如果这些 RSpec 测试在您的计算机上不起作用,那么您最好尝试一个新的 rails 项目(如果需要,您可以复制您的文件),并使用 Test::Unit guides.rubyonrails.org/testing.html 如果您决定这样做将 Capybara 和 RSpec 宝石从您的 Gemfile 中删除,您不需要这些。
    • 顺便说一句,您使用的是 Linux、Apple Mac 还是 Windows? -我的环境是Linux(Ubuntu)
    猜你喜欢
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 2020-12-31
    • 2020-06-19
    • 2021-10-08
    • 2012-06-09
    相关资源
    最近更新 更多