首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >失败/错误:get(“/指令/新”).should route_to("instructions#new")

失败/错误:get(“/指令/新”).should route_to("instructions#new")
EN

Stack Overflow用户
提问于 2016-11-17 08:24:11
回答 1查看 80关注 0票数 1

instructions_routing_spec.rb

代码语言:javascript
复制
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
end

instructions_controller.rb

代码语言:javascript
复制
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    
end

spec_helper.rb

代码语言:javascript
复制
# 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
end

routes.rb

代码语言:javascript
复制
  resources :instructions, :path => "help", :as => "help"
  resources :instructions, :only => [:index,:show]

rake路由输出

代码语言:javascript
复制
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,但是我不知道做错了什么,请帮助.好了!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-17 09:17:07

好吧,这里可能有几个问题。首先,要在matcher中使用参数,需要执行如下操作:

代码语言:javascript
复制
  get("/instructions/new/1").should route_to("instructions#new", id: 1)

这假定您已将路由指定为/:instructions/:new/:id

如果您的路由是/:instructions/:id/:new格式的,那么您自然需要相应地修改匹配器:

代码语言:javascript
复制
  get("/instructions/1/new").should route_to("instructions#new", id: 1)

但是,我觉得您一开始就设置了错误的路由--通常在调用new操作时,在控制器上,您不会为资源提供id (毕竟它还没有创建)。因此,你可能想要考虑改变路线,离开你的对手的原样。

您为rake路由提供的输出看起来不太正确,似乎缺少一些输出,但是您可能应该将路由更改为:

代码语言:javascript
复制
resources :instructions, :only => [:new, :index, :show]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40649850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档