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