如何使用rspec测试路由与控制器?

7

我只有一个规范,位于 spec/controllers/statuses_spec.rb

以下是它的内容:

require 'spec_helper'

describe StatusesController do
    describe "routing" do

    it "routes to #index" do
        get("/statuses").should route_to("statuses#index")
    end

  end
end

简单来说,我有一个简单的状态脚手架,并且状态控制器具有用于CRUD的标准操作,包括索引操作。
但是,当运行上述测试时,我得到了以下失败:
15:39:52 - INFO - Running: ./spec/controllers/statuses_spec.rb:6
Run options: include {:locations=>{"./spec/controllers/statuses_spec.rb"=>[6]}}
F

Failures:

  1) StatusesController routing routes to #index
     Failure/Error: get("/statuses").should route_to("statuses#index")
     ActionController::UrlGenerationError:
       No route matches {:controller=>"statuses", :action=>"/statuses"}
     # ./spec/controllers/statuses_spec.rb:8:in `block (3 levels) in <top (required)>'

Finished in 0.21772 seconds
1 example, 1 failure

Rspec假设我正在处理statuses控制器,这有点直观,因为我在我的规范的描述块中引用了它,并且它认为我传递给get方法的字符串(“/statuses”)是该函数。

说实话,我不太喜欢这个。 我想能够测试URL栏中确切的字符串是否发送到正确的控制器#操作对。 不管怎样,我按照rspec的说法进行:

require 'spec_helper'

describe StatusesController do
    describe "routing" do

    it "routes to #index" do
        get("index").should route_to("statuses#index")
    end

  end
end

然而,现在我得到了这个:
Run options: include {:locations=>{"./spec/controllers/statuses_spec.rb"=>[6]}}
F

Failures:

  1) StatusesController routing routes to #index
     Failure/Error: get("index").should route_to("statuses#index")
     NoMethodError:
       undefined method `values' for #<ActionController::TestResponse:0x00000102bd3208>
     # ./spec/controllers/statuses_spec.rb:8:in `block (3 levels) in <top (required)>'

Finished in 0.31019 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/controllers/statuses_spec.rb:6 # StatusesController routing routes to #index

我遇到了一个关于values方法的无法识别错误。Values?真的吗?我不知道为什么会出现这个错误。这是我的spec helper:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr
  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    Capybara.run_server = true
    Capybara.javascript_driver = :webkit
    Capybara.default_selector = :css
    Capybara.server_port = 7171
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.include RSpec::Rails::RequestExampleGroup, type: :feature

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"
end

路由规范不属于/controllers文件夹。 - apneadiving
1
阅读文档:https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/routing-specs - apneadiving
没错,尽管我对这个主题还很陌生,但这可能会帮助其他人,而且这个领域的文档相当零散(apneadiving的链接是第三方的)。真是喜欢这个网站! - Starkers
1
是的,-1 很严厉,我刚刚加了一个 +1,但要提醒自己要认真阅读 :) - apneadiving
3个回答

15

测试路由,尤其是标准的RESTful路由,不是标准做法。

a)您不想浪费重新测试Rails的路由功能的精力

b)当控制器或request规范无法路由请求时,它们应该失败

通常情况下,编写和维护路由测试并没有太大价值和增加信心。考虑在路由变得复杂和容易出错时进行测试。

话虽如此,RSpec提供了一个route_to匹配器来指定请求是否可路由。

推荐您的路由规范放在spec/routing下,尽管在控制器规范旁边看到路由规范并不罕见。例如:

describe VersionsController do
  describe 'routing' do
    it 'routes GET /version to VersionsController#show' do
      expect(get: '/version').to route_to(controller: 'versions', action: 'show')
    end
  end
end
shoulda-matchers gem有自己的路由匹配器,允许你编写类似以下测试的代码。
describe PostsController do
  it { should route(:get, '/posts').to(action: :index) }
  it { should route(:get, '/posts/1').to(action: :show, id: 1) }
end

谢谢。我认为通常不需要路由规范,但是当我们使用约束对象等进行奇怪的单元测试时,您是正确的。 - Chuck Vose

2

路由应该作为集成测试的一部分。集成测试是测试应用程序的重要工作流程的地方 - 更具体地说,是否定义了URL似乎是一个重要的工作流程。

您的集成测试看起来像任何正常的集成测试:

require 'test_helper'
class RoutesTest < ActionController::IntegrationTest
   test "route test" do
       assert_generates "/videos/5", { :controller => "videos", :action => "show", :id => "1" }
       assert_generates "/about", :controller => "pages", :action => "about"
   end
end

关于@jemminger的回答,他认为没有必要测试路径 - 虽然Rails的测试可以验证routes.rb是否正常工作,但Rails并不需要测试http://yoursite.com/users是否在你的路由中定义。但是,大多数路由测试可以在现有的集成测试中完成,因此特定的路由测试可能是冗余的。
我能想到的一个具体用例是那些已经或将要从Rails 2升级到Rails 3的用户。定义路由的代码已经发生了很大变化,最好通过测试来确保路由已经成功升级,而不是等到用户报告404错误时再进行修改。

0
it { expect(put: "/posts/1").to route_to(controller: "posts", action: "update", id: "1") }

it { expect(GET: "/posts/1").to route_to(controller: 'posts', action: 'show', id: "1") }

if { expect(get: '/posts').to route_to(controller: 'posts', action: 'index') }

如果没有删除路由
it { expect(delete: "/posts/1").to_not be_routable }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接