失败/错误:get(“/ instructions / new”)。应该route_to(“说明#new”)

时间:2016-11-17 08:24:11

标签: ruby-on-rails ruby rspec

instructions_routing_spec.rb

String val1 = map.get("iOS"); //this returns "100"
String val2 = map.get("Android"); // this returns "101"

instructions_controller.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
end

spec_helper.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    
end

的routes.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
end

佣金路线输出

  resources :instructions, :path => "help", :as => "help"
  resources :instructions, :only => [:index,:show]

有以下错误;

  

失败/错误:get(“/ instructions / new”)。应该   route_to( “指令#新”)          已识别的选项&lt; {“id”=&gt;“new”,“action”=&gt;“show”,“controller”=&gt;“说明”}&gt;不匹配&lt; {“action”=&gt;“new”,   “controller”=&gt;“说明”}&gt;,差异:&lt; {“id”=&gt;“new”,   “行动”=&gt; “中新”}取代。          &lt; {“action”=&gt;“new”,“controller”=&gt;“instructions”}&gt;预期但是          &lt; {“id”=&gt;“new”,“action”=&gt;“show”,“controller”=&gt;“说明”}&gt;。

     

注意: rpsec 2.11.0,rails 3.2.19,ruby 1.8.7

它应该像 /:controller /:action /:id 一样工作,但我不知道做错了什么,请帮助......!

1 个答案:

答案 0 :(得分:1)

好的,这里可能有几个问题。首先要在匹配器中使用参数,您需要执行以下操作:

  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]