instructions_routing_spec.rb
require "spec_helper"
describe InstructionsController do
before(:each) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.start
end
after(:each) do
DatabaseCleaner.clean
end
describe "routing" do
it "routes to #new" do
get("/instructions/new").should route_to("instructions#new")
end
end
endinstructions_controller.rb
class InstructionsController < ApplicationController
respond_to :html, :xml, :json
layout :single_column_layout
before_filter :admin_only, :except => [:show]
def new
@instruction = Instruction.new
respond_with(@instruction)
end
endspec_helper.rb
# 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 'forgery'
require 'populators'
# 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}
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
config.include Devise::TestHelpers, :type => :view
config.mock_with :rspec
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/test/fixtures"
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.after(:suite) do
DatabaseCleaner.clean
end
# 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 = false
endroutes.rb
resources :instructions, :path => "help", :as => "help"
resources :instructions, :only => [:index,:show]rake路由输出
instructions#destroy
instructions GET /instructions(.:format)
instructions#index
instruction GET /instructions/:id(.:format)有跟随错误的;
失败/错误:get(“/指令/新”).should route_to("instructions#new")已识别的选项<{“id”“=>”new“、”action“”=>“show”“>>不匹配<{”action“”=>“new”、“控制器”“=>”指令“}”}>、差异:<{"id"=>"new“、”action“”=>“new}>。<{“操作”“=>”“new”、“控制器”“=>”指令“}”>预期,但<{“id”“=>”new“、”action“”=>“show”、“=>”=>指令“}>。 注: rpsec 2.11.0,rails 3.2.19,ruby1.8.7
它应该像/:控制器/:action/:id,但是我不知道做错了什么,请帮助.好了!
发布于 2016-11-17 09:17:07
好吧,这里可能有几个问题。首先,要在matcher中使用参数,需要执行如下操作:
get("/instructions/new/1").should route_to("instructions#new", id: 1)这假定您已将路由指定为/:instructions/:new/:id。
如果您的路由是/:instructions/:id/:new格式的,那么您自然需要相应地修改匹配器:
get("/instructions/1/new").should route_to("instructions#new", id: 1)但是,我觉得您一开始就设置了错误的路由--通常在调用new操作时,在控制器上,您不会为资源提供id (毕竟它还没有创建)。因此,你可能想要考虑改变路线,离开你的对手的原样。
您为rake路由提供的输出看起来不太正确,似乎缺少一些输出,但是您可能应该将路由更改为:
resources :instructions, :only => [:new, :index, :show]https://stackoverflow.com/questions/40649850
复制相似问题