即使test.log显示ActionNotFound,RSpec测试也会通过

时间:2014-04-02 09:07:35

标签: ruby-on-rails ruby rspec capybara

我遵循M Hartl的“Ruby on Rails Tutorial”,第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测试&amp;结果:

$ 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'

1 个答案:

答案 0 :(得分:0)

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

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

另外rspec-rails gem版本在第2版的Gemfile中有所不同。因此,如果上述更改无法正常工作,请尝试以下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