gpt4 book ai didi

ruby-on-rails - 我应该如何使用 rspec 测试路由和 Controller ?

转载 作者:行者123 更新时间:2023-12-04 22:47:10 25 4
gpt4 key购买 nike

我只有一个规范,位于 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

可以说,我有一个简单的状态脚手架,状态 Controller 具有 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 Controller ,我想这有点直观,因为我在规范的描述块中引用了它,它认为我传递给 get 方法 ('/statuses') 的字符串是函数。

坦白说,我真的不喜欢这个。我希望能够测试 URL 栏中的确切字符串将转到正确的 controller#action 对。无论如何,我按照 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 的无方法错误方法。值(value)观?说真的,只是什么?我不知道为什么会出现此错误。这是我的规范助手:
# 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

最佳答案

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

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

b) 您的 Controller 或 request规范在无法路由请求时应该失败

更常见的是,编写和维护路由测试不会带来太多值(value)和增加信心。
当路由变得复杂且容易出错时,请考虑测试路由。

也就是说,RSpec 提供了 route_to matcher用于指定请求是可路由的。

路由规范的推荐位置在 spec/routing 下,尽管在 Controller 规范旁边看到路由规范并不少见。例如

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

关于ruby-on-rails - 我应该如何使用 rspec 测试路由和 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20197271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com