23

I am writing a spec to test the behavior of the mashup_controller when someone sends a query through a URL. I need to simulate the parameters contained in the URL, and i read that the post() method will do that, however when i get an error:

1) MashupController simulates query
     Failure/Error: post :create
     NoMethodError:
       undefined method `post' for
#<RSpec::Core::ExampleGroup::Nested_1:0x980bc50>
     # ./mashup_controller_rspec.rb:9:in `block (2 levels) in <top (required)>'

Finished in 0.20199 seconds 1 example, 1 failure

Failed examples:

rspec ./mashup_controller_rspec.rb:7 # MashupController simulates query

Here is my code:

require 'spec_helper'
require 'mashup_controller.rb'

describe MashupController do
    it "simulates query" do
        post :create    
    end
end

Sorry if I'm not making any sense. I am very new to rails and rspec. Any help would be appreciated. Thanks.

user727403
  • 1,967
  • 3
  • 13
  • 14
  • 1
    Show your directory structure. Also, what rails version are you using? Does this mashup controller inherits from ApplicationController? – Maurício Linhares Aug 21 '11 at 03:09
  • yeah, MashupController does inherit from ApplicationController. mashup_controller.rb is in another directory, but I added it to $LOAD_PATH, so i dont think that is the problem. Also, I am running Ruby 1.9.2, Rails 3.0.1, and rspec-rails 2.6.1. I am wondering if i just set something up wrong. – user727403 Aug 21 '11 at 19:28

5 Answers5

34

If the spec file is not under spec/controllers, methods like get and post will not be automatically made available by rspec-rails.

You either need to tag your spec:

describe MyController, type: :controller do
  # ...
end

or include the module:

describe MyController do
  include RSpec::Rails::ControllerExampleGroup
  # ...
end

See the relevant code in rspec-rails.

Leventix
  • 3,789
  • 1
  • 32
  • 41
3
  1. Make sure you have gem spec-rails in your Gemfile
  2. Your mashup_controller_rspec.rb should be under spec/controllers
Uko
  • 13,134
  • 6
  • 58
  • 106
1

I used gem rspec-rails instead of gem spec-rails.

Arman Ortega
  • 3,003
  • 1
  • 30
  • 28
1

In Rails 4, you can declare the type of the RSpec tests as :request and the spec file can be in any directory.

example: in spec/routes/users.rb
RSpec.describe 'UserRoutes', type: :request do
  ...
end
Zoe
  • 27,060
  • 21
  • 118
  • 148
RoundOutTooSoon
  • 9,821
  • 8
  • 35
  • 52
0

My solution is

describe MyController, type: :controller
...
end
Hiep Dinh
  • 654
  • 5
  • 8