Zain Abbas Zain Abbas - 17 days ago 5
Ruby Question

Failure/Error: get("/instructions/new").should route_to("instructions#new")

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
end


instructions_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
end


spec_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
end


routes.rb

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


rake routes output

instructions#destroy
instructions GET /instructions(.:format)
instructions#index
instruction GET /instructions/:id(.:format)


Having Following Error;


Failure/Error: get("/instructions/new").should
route_to("instructions#new")
The recognized options <{"id"=>"new", "action"=>"show", "controller"=>"instructions"}> did not match <{"action"=>"new",
"controller"=>"instructions"}>, difference: <{"id"=>"new",
"action"=>"new"}>.
<{"action"=>"new", "controller"=>"instructions"}> expected but was
<{"id"=>"new", "action"=>"show", "controller"=>"instructions"}>.

Note: rpsec 2.11.0, rails 3.2.19, ruby 1.8.7


It should work like /:controller/:action/:id but I dont know what am doing wrong please help .... !

Answer

Ok, there may be a couple of issues here. Firstly to use parameters in the matcher you would need to do something like the following:

  get("/instructions/new/1").should route_to("instructions#new", id: 1)

This assumes you have specified your route as /:instructions/:new/:id

If your route is of the form /:instructions/:id/:new then naturally you would need to modify the matcher accordingly:

  get("/instructions/1/new").should route_to("instructions#new", id: 1)

However, I feel you have set up your route wrong in the first place - normally when you call the new action, on a controller, you wouldn't be providing an id for the resource (after all it hasn't yet been created). So you might want to consider changing the route and leaving your matcher as is.

The output you have supplied for rake routes doesn't look right, seems some of it is missing, however you should probably change your routing to the following:

resources :instructions, :only => [:new, :index, :show]
Comments