2

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

jonnyjava.net
  • 912
  • 7
  • 23
  • possible duplicate of [problem with rspec test, undefined method 'post'](http://stackoverflow.com/questions/7135377/problem-with-rspec-test-undefined-method-post) – Brad Werth Jan 16 '15 at 17:49
  • Of course [that solution](http://stackoverflow.com/questions/7135377/problem-with-rspec-test-undefined-method-post) does not work for me – jonnyjava.net Jan 16 '15 at 18:06
  • are you using the rspec-rails gem? – Anthony Jan 19 '15 at 13:04
  • Possible duplicate of [undefined method `get' for #](http://stackoverflow.com/questions/6296235/undefined-method-get-for-rspeccoreexamplegroupnested-10x00000106db51f) – Engr. Hasanuzzaman Sumon Nov 02 '16 at 12:25

1 Answers1

1

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.

Bachet
  • 321
  • 1
  • 2
  • 14