2
votes

I have this route:

post 'create', to: 'applications#create'

this controller:

class ApplicationsController < ApplicationController
  def create
  end
end

and this test:

require "spec_helper"

describe ApplicationsController do
  describe "routing" do
    it 'routes to #create' do
      post('/applications').should route_to('applications#create')
    end
  end
end

and this is my spec_helper:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|

  config.expect_with :rspec do |c|
    c.syntax = :should
  end
  config.mock_with :rspec do |c|
    c.syntax = :should
  end
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
  config.order = "random"
end

Why when I run

rspec ./spec/routing

I get this error?

Failure/Error: post('/applications').should route_to('applications#create') NoMethodError: undefined method `post' for RSpec

I tried changing the test using expect but with no luck. neither with a different class name.

What should I do to test my routes?

UPDATE

If I do this:

it "should return status 200" do
  post('/applications'), {}
  response.status.should be(200)
end

I get the same error

1
Of course that solution does not work for mejonnyjava.net
are you using the rspec-rails gem?Anthony

1 Answers

1
votes

I am not sure it can help you but I had the same problem when passing to Rspec 3

According to https://relishapp.com/rspec/rspec-rails/docs/directory-structure

we should enable that option to pass automatically metadata.

​# spec/rails_helper.rb
RSpec.configure do |config|
  config.infer_spec_type_from_file_location!
end

Once this was added my specs passed.