Sta*_*ers 4 rspec ruby-on-rails capybara
我只有一个规格,位于 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
Run Code Online (Sandbox Code Playgroud)
可以说,我有一个简单的状态脚手架,状态控制器具有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
Run Code Online (Sandbox Code Playgroud)
Rspec假设我正在处理statuses控制器,我觉得这很直观,因为我在我的spec的describe块中引用它,它认为我传入get方法的字符串('/ statuses')是功能.
坦率地说,我真的不喜欢这个.我希望能够测试URL栏中的确切字符串是否转到右侧控制器#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 Code Online (Sandbox Code Playgroud)
但是,现在我明白了:
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
Run Code Online (Sandbox Code Playgroud)
关于values方法,我得到一个no方法错误.值?说真的,到底是什么?我不知道为什么我会收到这个错误.这是我的规范助手:
# 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
Run Code Online (Sandbox Code Playgroud)
测试路由,尤其是标准RESTful路由,不是标准做法.
a)您不想浪费时间重新测试Rails的路由功能
b)当您的控制器或请求规范无法路由请求时,它们将失败
更常见的是,编写和维护路由测试不会带来太多价值和增加信心.考虑在路线变得复杂且容易出错时进行测试.
也就是说,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
Run Code Online (Sandbox Code Playgroud)
该早该-的匹配宝石都有其自己的路线匹配,让你写检查,如
describe PostsController do
it { should route(:get, '/posts').to(action: :index) }
it { should route(:get, '/posts/1').to(action: :show, id: 1) }
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8153 次 |
| 最近记录: |